VB編程中的一些經驗
發表時間:2024-02-25 來源:明輝站整理相關軟件相關文章人氣:
[摘要]1. 假設VB中有如下的變量聲明: dim s1, s2 as string則s1是一個variant型變量,而s2是一個string型變量如果想要聲明兩個string變量s1和s2應該用: dim s1 as string, s2 as string2. VB中的對象是自動回收的,類似java在...
1. 假設VB中有如下的變量聲明:
dim s1, s2 as string
則s1是一個variant型變量,而s2是一個string型變量
如果想要聲明兩個string變量s1和s2
應該用:
dim s1 as string, s2 as string
2. VB中的對象是自動回收的,類似java
在一個過程中
sub Foo()
dim obj as new Object
.... 'do something with obj
end sub
過程結束的時候沒有必要 set obj = nothing
因為當離開該過程的時候,局部變量obj消失,因此對Object對象實例的引用也就消失(除非在過程內部有其他的全局變量引用了該對象的實例),所以對象實例會自動釋放
但是對于模塊級的對象變量,new了一個實例后用完了必須set obj = nothing來釋放該實例
3. 對對象變量賦值應該用 set obj = AnOtherObj 這種方式,相當于讓obj指向AnOtherObj所指向的對象。VB中的對象變量實質上是一個指向對象實例的指針,這點和java,pascal相同,和C++中不同
4. VB中字符串的內部存儲格式是Unicode,它可以自動轉化為ANSI字符(單字節字符)或者 DBCS 字符(雙字節字符)。例如
dim s1 as string, s2 as string
s1 = "中文"
s2 = left(s1, 1)
則得到的實際上是 s2 = "中"
因為VB會自動將s1內部unicode的"中"字單作一個DBCS字符取出來傳給s2
因此處理雙字節字符的時候特別要注意,很容易產生數組溢出錯誤
VB中的常用字符串處理函數,例如Asc, Left, Mid...都會自動判斷處理的字符串中的每個字符是單字節還是雙字節(因為字符串在內部以Unicode存儲,所以這一點很容易做到),然后自動轉化為ANSI字符或DBCS字符。
5. 字符串的比較應該是用 strCmp 函數,而不是簡單的用 = 號
StrComp(string1, string2[, compare])
其中參數compare的取值含義如下:
常量 值 含義
vbUseCompareOption -1 根據Option Compare 語句的設定進行字符串比較
vbBinaryCompare 0 進行二進制比較,也就是將string1和string2中的unicode字符看作數組進行字典序比較
vbTextCompare 1 進行文本比較
vbDatabaseCompare 2 這個選項只適用于Microsoft Access,根據數據庫的設定進行比較
對于英文字符串進行 vbTextCompare 比較時,不區分字母大小寫,例如: "a" 與 "A" 相等;
對于中文或其他雙字節字符串進行 vbTextCompare 比較時,不區分全角字符和半角字符,例如 "A", "A", "a", "a" 都相等;
6. VB中字符串處理的函數有三種版本:
(1) ANSI和DBCS版本,一般的字符串函數(例如Mid(), Left(), ... )都是該版本,該版本的函數可以自動識別ANSI字符和DBCS字符,而且無論是ANSI字符還是DBCS字符都當作一個字符處理(雖然一個DBCS字符是兩個字節,但還是當作一個字符處理)
(2) 二進制版本,這個版本的函數是在第一類函數的名稱后面加上B, 例如 MidB(), LeftB()……;這個版本的函數將字符串當作字節數組處理,例如 s = "abc", k = LenB(s) , 則 k=6,因為字符串在VB內部以unicode存儲,而一個unicode字符占兩個字節,所以s實際上占用了2*3=6個字節的空間,于是LenB(s)返回6
(3) Unicode版本,這個版本的函數是在第一類函數名稱后面加上W,例如AscW, ChrW;這個版本的函數將字符串當作unicode處理。
函數 功能描述
Asc Returns the ANSI or DBCS character code for the first character of a string.
AscB Returns the value of the first byte in the given string containing binary data.
AscW Returns the Unicode character code for the first character of a string.
Chr Returns a string containing a specific ANSI or DBCS character code.
ChrB Returns a binary string containing a specific byte.
ChrW Returns a string containing a specific Unicode character code.
Input Returns a specified number of ANSI or DBCS characters from a file.
InputB Returns a specified number of bytes from a file.
InStr Returns the first occurrence of one string within another.
InStrB Returns the first occurrence of a byte in a binary string.
Left,Right Returns a specified number of characters from the right or left sides of a string.
LeftB, RightB Returns a specified number of bytes from the left or right side of a binary string.
Len Returns the length of the string in number of characters.
LenB Returns the length of the string in number of bytes.
Mid Returns a specified number of characters from a string.
MidB Returns the specified number of bytes from a binary string.
7. VB程序代碼中的以下標識符不能含有雙字節字符:
Public procedure names 公共過程名稱
Public variables 公共變量
Public constants 公共常量
Project name (as specified in the Project Properties dialog box) 工程名稱(在工程屬性對話框中的名字) [PS: VB中文版中的工程名稱可以是中文,比較奇怪的說]
Class names (Name property of a class module, a user control, a property page, or a user document) 類名(類模塊、用戶控件模塊,屬性頁,用戶文檔的name屬性)
換句話說,其他的標識符都可以用中文,例如:
Private Sub cmdTest_Click()
Dim 中文變量 As String
中文變量 = "你好! hello!"
MsgBox 中文變量
End Sub
這樣的代碼也是合法的 :-)