Announcement

Collapse
No announcement yet.

Select CASE between values

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

  • Select CASE between values

    Hi guys

    This doing my head in. This is an ASP prob.

    I have

    Code:
    val1 = 1000
    val2 = 500
    val3 = 1500
    
    select case val1
    case val1 > val2, val1 < val3
    response.write "Huuray!"
    end select
    Why doesn't this produce the result of Huuray!???

    Im missing something simple i know i am!!
    Server 2000 MCP
    Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

  • #2
    Re: Select CASE between values

    ASP doesn't use full blown VB. You get the same features as VBScript. So all you get is the ability to "case" discrete values vs. ranges.

    So you COULD resort to good old if-then-else logic:

    In your "case" below, try:

    if val1 > val2 or val1 < val3 then
    response.write "Hurray!"
    end if
    You can slap some parens around the >, < tests if it makes you feel better.

    OR you could think a little bit in reverse and get this sorry "select case" construct to work:

    val1 = 1000
    val2 = 500
    val3 = 1500

    select case true
    case val1 > val2, val1 < val3
    response.write "Hurray!"
    end select
    Which, at run time, resolves to:

    select case true
    case true, true
    response.write "Hurray!"
    end select
    And it works!!! Adjust your values for the variables to convince yourself.

    Here's the syntax for each per the MS help file for Visual Basic Scripting Edition:

    Executes one of several groups of statements, depending on the value of an expression.

    Select Case testexpression
    [Case expressionlist-n
    [statements-n]] . . .
    [Case Else
    [elsestatements-n]]
    End Select

    Arguments

    testexpression
    Any numeric or string expression.

    expressionlist-n
    Required if Case appears. A comma delimited list of one or more expressions.

    statements-n

    Remarks

    If testexpression matches any Case expressionlist expression, the statements following that Case clause are executed up to the next Case clause, or for the last clause, up to End Select. Control then passes to the statement following End Select. If testexpression matches an expressionlist expression in more than one Case clause, only the statements following the first match are executed.

    The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist in any of the other Case selections. Although not required, it is a good idea to have a Case Else statement in your Select Case block to handle unforeseen testexpression values. If no Case expressionlist matches testexpression and there is no Case Else statement, execution continues at the statement following End Select.

    Select Case statements can be nested. Each nested Select Case statement must have a matching End Select statement.

    The following example illustrates the use of the Select Case statement.

    Dim Color, MyVar
    Sub ChangeBackground (Color)
    MyVar = lcase (Color)
    Select Case MyVar
    Case "red" document.bgColor = "red"
    Case "green" document.bgColor = "green"
    Case "blue" document.bgColor = "blue"
    Case Else MsgBox "pick another color"
    End Select
    End Sub
    Conditionally executes a group of statements, depending on the value of an expression.

    If condition Then statements [Else elsestatements ]
    ' Or, you can use the block form syntax:
    If condition Then
    [statements]
    [ElseIf condition-n Then
    [elseifstatements]] . . .
    [Else
    [elsestatements]]
    End If

    Arguments

    condition
    One or more of the following two types of expressions:

    A numeric or string expression that evaluates to True or False. If condition is Null, condition is treated as False.

    An expression of the form TypeOf objectname Is objecttype. The objectname is any object reference and objecttype is any valid object type. The expression is True if objectname is of the object type specified by objecttype; otherwise it is False.

    statements
    One or more statements separated by colons; executed if condition is True.

    condition-n
    Same as condition.

    elseifstatements
    One or more statements executed if the associated condition-n is True.

    elsestatements
    One or more statements executed if no previous condition or condition-n expression is True.

    Remarks

    You can use the single-line form (first syntax) for short, simple tests. However, the block form (second syntax) provides more structure and flexibility than the single-line form and is usually easier to read, maintain, and debug.

    Note
    With the single-line syntax, it is possible to have multiple statements executed as the result of an If...Then decision, but they must all be on the same line and separated by colons, as in the following statement:

    If A > 10 Then A = A + 1 : B = B + A : C = C + B

    When executing a block If (second syntax), condition is tested. If condition is True, the statements following Then are executed. If condition is False, each ElseIf (if any) is evaluated in turn. When a True condition is found, the statements following the associated Then are executed. If none of the ElseIf statements are True (or there are no ElseIf clauses), the statements following Else are executed. After executing the statements following Then or Else, execution continues with the statement following End If.

    The Else and ElseIf clauses are both optional. You can have as many ElseIf statements as you want in a block If, but none can appear after the Else clause. Block If statements can be nested; that is, contained within one another.

    What follows the Then keyword is examined to determine whether or not a statement is a block If. If anything other than a comment appears after Then on the same line, the statement is treated as a single-line If statement.

    A block If statement must be the first statement on a line. The block If must end with an End If statement.
    Cheers,

    Rick

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

    Comment


    • #3
      Re: Select CASE between values

      Cheers Rick - i went for the IF statement solution. I read somewhere that the slect case in vb script cannot compare multiple sets of variables to get a case result... odd but anyway it works now. Thanks.
      Server 2000 MCP
      Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

      Comment


      • #4
        Re: Select CASE between values

        Originally posted by tonyyeb View Post
        I read somewhere that the slect case in vb script cannot compare multiple sets of variables to get a case result
        Love to see where that is stated in that way. I'd understand if the reference mentioned range comparisons. In fact that would be something I would have said as I had always assumed these discrete expression lists would consist of literals or functions, not Boolean comparison expressions.

        Until you put up this post, I would have gone w/ "if-then-else" every time. Looking at your code snippet made me think about how val1 was being compared to (val1 > val2) or (val1 < val3) and it hit me.

        Flipping the thinking around with a discrete test expression of "true" and numerous Boolean expressions in the expression lists of the required complexity is a novel way to get true "select case" functionality in VBScript.

        I'm 99.44% sure this trick will work to map any select statement from true VB to VBScript. At least that's my story.
        Cheers,

        Rick

        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

        2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

        Comment

        Working...
        X