我有段程序,语句格式大概是这样if x="str1" or x="str2" or x="str3" or x="str4" or x="str5" or x="str6" then执行····end if这样的结构,程序会连续判断6个条件,我想让程序只要1个条件符合,就执行命令,不需要再判断后面的条件了
我自己测试过这样的结构if x="str1" 如果这个条件成立,但是还会去判断后面的 我自己这样写Private Sub Command1_Click() If Text1 = "1" Or Text1 = "2" Or Text1 = xx Then MsgBox "qqqq" End If End Sub Private Function xx() As String xx = "3" MsgBox "3" End Function测试后,第1个条件如果成立,还是会继续判断后面的程序···
or 的话就这样可以了,如果是 and 就分多个 if 语句会高效点
效率差不了多少,and的话有区别。
如果楼主的判断的有规律, 试试 If x Like "str[1-6]" then 速度不清楚。
vb的判断不同其它的一些语言,但如果一定要实现你的要求也可以,把所有条件重新拆开来就是了,就怕到时你又觉得烦:if x="str1" then 执行···· goto ok end ifif x="str2" then 执行···· goto ok end ifif x="str3" then 执行···· goto ok end if 。 。 。 。 ok: '接着执行。
下面这个意思吧?不过效率与if ...or ...then比较到底如何,要测试function InString(s as String,a as variant) as boolean dim i as long for i=0 to ubound(a) if s=a(i) then InString=true exit function end if next end function'调用 debug.? InString("Str2",array("str1","Str2","str3","str1","str5","str6"))
不知这样是不是更有效率?Option ExplicitPrivate Sub Form_Load() Dim intP As Integer Dim strP As String strP = "A" intP = Int(strP = "A") + Int(strP = "B") + Int(strP = "C") + Int(strP = "D") If intP <> 0 Then
End If End Sub
Private Sub Command1_Click() Dim x As String, iFlg As Boolean x = "str2" If x = "str1" Then iFlg = True ElseIf x = "str2" Then iFlg = True ElseIf x = xx Then iFlg = True End If If iFlg Then Debug.Print "xxxxx" End IfEnd SubPrivate Function xx() As String xx = "3" MsgBox "3" End Function
or是或操作
在VB6中,像这样的语句和C以及其他语言是不一样的,即使 x="str1"了,VB6依旧会执行后面的五项判断。像这样的判断,在VB6中应该说无法优化了。一定程度上,VB6这点不仁道。
我自己这样写Private Sub Command1_Click()
If Text1 = "1" Or Text1 = "2" Or Text1 = xx Then
MsgBox "qqqq"
End If
End Sub
Private Function xx() As String
xx = "3"
MsgBox "3"
End Function测试后,第1个条件如果成立,还是会继续判断后面的程序···
试试
If x Like "str[1-6]" then
速度不清楚。
执行····
goto ok
end ifif x="str2" then
执行····
goto ok
end ifif x="str3" then
执行····
goto ok
end if
。
。
。
。
ok:
'接着执行。
dim i as long
for i=0 to ubound(a)
if s=a(i) then
InString=true
exit function
end if
next
end function'调用
debug.? InString("Str2",array("str1","Str2","str3","str1","str5","str6"))
,str1,str2,str3,str4,str5,str6,
然后用instr查找“,”+x+","是否大于0,注意“,”符号不能出现在str1 str2 str3 str4 str5 str6和X中。
select case x
case "str1","str2","str3","str4","str5","str6"
'...
end select
Dim intP As Integer
Dim strP As String
strP = "A"
intP = Int(strP = "A") + Int(strP = "B") + Int(strP = "C") + Int(strP = "D")
If intP <> 0 Then
End If
End Sub
Private Sub Command1_Click()
Dim x As String, iFlg As Boolean
x = "str2"
If x = "str1" Then
iFlg = True
ElseIf x = "str2" Then
iFlg = True
ElseIf x = xx Then
iFlg = True
End If
If iFlg Then
Debug.Print "xxxxx"
End IfEnd SubPrivate Function xx() As String
xx = "3"
MsgBox "3"
End Function