關於全域性變數的定義

2022-08-19 14:57:07 字數 859 閱讀 1547

在用vb開發軟體時,經常需要在不同的窗體間共享資料,但在過多的使用全域性變數時不便於軟體的除錯和修改。通常有兩種解決方法。

第一種方法:定義全域性變數,然後在各個窗體中直接使用,例如

public strcustomerid as string

public strcustomername as string

第二種方法:新增乙個模組專門用於定義變數,然後定義一些public過程或函式來設定和獲取這些資料,例如

dim strcustomerid as string

dim strcustomername as string

public sub setcustomerid(customerid as string)

strcustomerid = customerid

end sub

public function getcustomerid() as string

getcustomerid = strcustomerid

end function

public sub setcustomername(customername as string)

strcustomername = customername

end sub

public function getcustomername() as string

getcustomername = strcustomername

end function

其實這兩種方法都有缺點,使用第一種方法時,當過程或函式中過於頻繁的使用全域性變數時會給除錯和修改程式帶來很大的工作量,有時會產生災難性的後果。使用第二種方法時,若共享資料很多,則需要定義很多的set*和get*函式。

全域性變數巨集定義 精

以下是如何定義全域性變數。眾所周知,全域性變數應該是得到記憶體分配且可以被其他模組通過c語言中extern關鍵字呼叫的變數。因此,必須在 c 和 h 檔案中定義。這種重複的定義很容易導致錯誤。error e46 undefined external serbfr referred in zlg522...

全域性變數 區域性變數 靜態全域性變數 靜態區域性變數的區別

118848 0500 20 0other 15 013 868 00 0星級blog 0gyan ffffff 187218 no repeat blank 144143 02010 09 13 21 33 41 144141 02010 09 13 21 33 31 20100913 20100...

C中怎樣定義和宣告全域性變數和函式最好

首先,儘管乙個全域性變數或函式可以 在多個編譯單元中 有多處 宣告 但是 定義 卻只能允許出現一次。定義是分配空間並賦初值 如果有 的宣告。最好的安排是在某個相關的.c 檔案中定義,然後在標頭檔案 h 中進行外部宣告,在需要使用的時候,只要包含對應的標頭檔案即可。定義變數的.c 檔案也應該包含該標頭...