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