VB编程中的一些经验

类别:VB语言 点击:0 评论:0 推荐:

VB编程中的一些经验

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

这样的代码也是合法的 :-)

本文地址:http://com.8s8s.com/it/it6417.htm