VB编程中的一些经验[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 starfish 的 blog

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  公共过程名称

本文关键:VB, 经验
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top