|
1、下列代码,则是对逻辑运算不清楚造成
If A=true Then
C= Not B
Else
C= B
End If
可以: C=A XOR B
2、如果加上下列代码:
If C=true then
D=28
Else
D=29
End IF
D=Iif((A XOR B),28,29)
3、布尔赋值,常被人忽略,如:
If A= 13 then
B=True
Else
B=False
End If
可以: B = A = 13 或者: B = (A = 13) 我更喜欢用后者,这样代码易于看懂。
4、字串有效性检测:
If IsNull(StrOrg) or StrOrg=”” then 可以: If Len(StrOrg & “”)<>0 then
5、字串重复次数
RepeatCount=Ubound(Split(StrOrg,StrFind)) 同样,如果要对字串有效性判断: RepeatCount=Iif((Len(StrOrg & “”)=0), 0, Ubound(Split(StrOrg,StrFind))
6、有时需要判断字串数组中是否有这一元素,这时最好不用数组,而用分隔符字串,于是:
If Len(OrgStr)= Len(Replace(OrgStr,FindStr)) then 则表明,此元素不存在。
7、对数组初始化,最好用变体,这样,也是一行语句,如:
IntArr=Array(12,28,29,30,31,52,24,60) 注意,此时需要用变量后缀。上面代码,如要定义为长整型,则 IntArr=Array(12&,28&,29&,30&,31&,52&,24&,60&) 要将IntArr 定义为变体
8、判断大小:
IntMax = Iif((IntA > IntB), IntA, IntB) IntMin = Iif((IntA < IntB), IntA, IntB) 9、按索引的Select Case Function GetChoice(Ind As Integer) GetChoice = Choose(Ind, "Speedy", "United", "Federal") End Function 10、按表达式的Select Case(这种转换要求不能有Case Else的才可以这样,否则会出错) Function MatchUp (CityName As String) Matchup =tch(CityName = "London", "English", CityName _ = "Rome", "Italian", CityName = "Paris", "French") End Function 11、使用Iif,前面已有 Function CheckIt (TestMe As Integer) CheckIt = IIf(TestMe > 1000, “Large”, “Small”) End Function
12、字串动态数组是否已初始化
If Len(Join(StrArr))=0 then 字串动态数组未初始化。
13、指定只读CombBox的当前值,如果能确认这个值就在其中,一定不会错,则:
Combbox=CurValue 注意,不可以写成: Combbox.text=CurValue 前者实际是写 _default 这个属性,而后者则是写Text 因为只读,则会导致错误
14、如果有下列代码:
Select Case CombBox.text
Case “London”
Call FuncStrLang(3)
Case “Rome”
Call FuncStrLang(5)
……
End Select
则可以用ItemData属性,即: “London” 的 Itemdata=3 “Rome” 的 Itemdata=5 于是: Call FuncStrLang(CombBox.ItenData)
15、如果有下列代码:
Select Case CombBox.text
Case “London”
Call ClsCity.CityIntr_London
Case “Rome”
Call ClsCity.CityIntr_Rome
……
End Select
只要: CallByName ClsCity, “CityIntr_” & CombBox.text, vbMethod
16、复制数组到另一变量中: Dim iOrgArr(30) as Integer Dim iDesArr as Variant …… iDesArr = iOrgArr 即主变体直接取数组指针,则所有元素都复制了过去。
17、如果有下列代码:
Do While Not RsAdo.Eof If len(DesStr)<>0 then DesStr=DesStr & VbTab End if DesStr=RsAdo!Rec_id RsAdo.MoveNext loop
|