Announcement

Collapse
No announcement yet.

VBScript - Select Case for 2 of the same options?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBScript - Select Case for 2 of the same options?

    I have got a select case statement in which I need two or more options to be mapped to the same action, something like
    Code:
    Select Case strOption
        Case "abc" or "hjk"
            do something
        Case "xyz" or "sdf"
            do something else
        Case Else
            do last resort.
    End Select
    I really really don't want to have to have a separate Case block for each option as this would double all the code for each option.

    I have checked the docs but cannot see a way of doing multiple cases at the same time.

    Any guidance on this would be much appreciated.

  • #2
    Re: VBScript - Select Case for 2 of the same options?

    Use comma to separate the expressions not OR


    Code:
    strOption = "sDf"
    
    Select Case LCase(strOption) 
    ' Case.description = "show popups" 
    
        Case "abc","hjk" call cMatch(1, strOption)
    
            wscript.echo "Select Case ends after the first hit"
    
    
        Case "xyz","sdf","rrw","yeu", _
              "bdr","fas","skw","lmn" call cMatch(2, strOption)
    
            wscript.echo "Select Case ends after the first hit"
            
    
        Case Else call cMatch(3, strOption)
    
            wscript.echo "did last resort."
    
    End Select
    
    
    ''''''''''''''''''''     Or else...
    
    
    varVal = LCase(strOption)
    
    Select Case True 
    ' Case.description = "show another popup" 
    
        Case varVal="abc" OR varVal="hjk"
            wscript.echo "Or something"
    
        Case varVal="xyz" OR varVal="sdf" _
              OR varVal="rrw" OR varVal="yeu" _
               OR varVal="bdr" OR varVal="fas" _
                OR varVal="skw" OR varVal="lmn"
            wscript.echo "Or something else"
    
        Case Else
            wscript.echo "no last resort."
    
    End Select
    
    
    '''''''''''''''''''' 
    wscript.quit
    
    
       private Sub cMatch(casenr, val)
         Select Case casenr
    
           Case 1
            wscript.echo "begin", val
            wscript.echo "do something"
    
           Case 2
            wscript.echo "begin", val
            wscript.echo "do something else"
    
           Case 3 'Else
            wscript.echo "begin", val
            wscript.echo "do last resort."
    
         End Select
       End sub
    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: VBScript - Select Case for 2 of the same options?

      thanks that's what I am doing now, it just wasn't listed in the example I looked at on mdsn, should have more carefully read all surrounding text...

      Comment

      Working...
      X