我有段程序,语句格式大概是这样if x="str1" or x="str2" or x="str3" or x="str4" or x="str5" or x="str6" then执行····end if这样的结构,程序会连续判断6个条件,我想让程序只要1个条件符合,就执行命令,不需要再判断后面的条件了

解决方案 »

  1.   

    说错了,要不用多个if得了,层次分明点.
    or是或操作
      

  2.   


    在VB6中,像这样的语句和C以及其他语言是不一样的,即使 x="str1"了,VB6依旧会执行后面的五项判断。像这样的判断,在VB6中应该说无法优化了。一定程度上,VB6这点不仁道。
      

  3.   

    我自己测试过这样的结构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个条件如果成立,还是会继续判断后面的程序···
      

  4.   

    or 的话就这样可以了,如果是 and 就分多个 if 语句会高效点
      

  5.   

    效率差不了多少,and的话有区别。
      

  6.   

    如果楼主的判断的有规律,
    试试
    If x Like "str[1-6]" then
    速度不清楚。
      

  7.   

    vb的判断不同其它的一些语言,但如果一定要实现你的要求也可以,把所有条件重新拆开来就是了,就怕到时你又觉得烦:if x="str1"  then
    执行····
    goto ok
    end ifif  x="str2"  then
    执行····
    goto ok
    end ifif  x="str3"  then
    执行····
    goto ok
    end if




    ok:
    '接着执行。
      

  8.   

    下面这个意思吧?不过效率与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"))
      

  9.   

    另一个办法是将你的str1,str2之类join起来,变成这个样子:
    ,str1,str2,str3,str4,str5,str6,
    然后用instr查找“,”+x+","是否大于0,注意“,”符号不能出现在str1 str2 str3 str4 str5 str6和X中。
      

  10.   

    个人认为尽量用if ...or ...then...,除非你要比较的字符串特别多,代码可读性实在太差,或者很多地方要这样比较,再考虑用自己写的函数...
      

  11.   

    看情况,或者用select case语句,也是一种不错的选择..
      

  12.   

    select case语句可以判断字符串吗?我只对数字类型的用过
      

  13.   

    试试看吧
    select case x
    case "str1","str2","str3","str4","str5","str6"
        '...
    end select
      

  14.   

    你对VB的效率要求太极致了,用VB没必要这样抠效率,即使有or判断的优化效率办法对程序的改善也是微乎其微的。如果对效率要求很高,请转向C。
      

  15.   

    不知这样是不是更有效率?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
      

  16.   


    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