Avatar of freebullets
freebullets asked on

Why Won't This "Select Case" Work?

The following Select Case will always goes to Case Else.  "a" is always what it was before the Case is executed.  "a" is a Decimal.  "b" is an Object.
Select Case a
            Case a >= 100
                b.Image = Change_Calculator.My.Resources.Resources.hundred
                a = a - 100
            Case a >= 50
                b.Image = Change_Calculator.My.Resources.Resources.fifty
                a = a - 50
            Case a >= 20
                b.Image = Change_Calculator.My.Resources.Resources.twenty
                a = a - 20
            Case a >= 10
                b.Image = Change_Calculator.My.Resources.Resources.ten
                a = a - 10
            Case a >= 5
                b.Image = Change_Calculator.My.Resources.Resources.five
                a = a - 5
            Case a >= 1
                b.Image = Change_Calculator.My.Resources.Resources.one
                a = a - 1
            Case a >= 0.25
                b.Image = Change_Calculator.My.Resources.Resources.quarter
                a = a - 0.25
            Case a >= 0.1
                b.Image = Change_Calculator.My.Resources.Resources.dime
                a = a - 0.1
            Case a >= 0.05
                b.Image = Change_Calculator.My.Resources.Resources.nickel
                a = a - 0.05
            Case a >= 0.01
                b.Image = Change_Calculator.My.Resources.Resources.penny
                a = a - 0.01
            Case Else
                MessageBox.Show("Case Else on Select Case a.  ""a"" is currently: " & a)
        End Select

Open in new window

Visual Basic.NETVisual Basic ClassicRegular Expressions

Avatar of undefined
Last Comment

8/22/2022 - Mon

from here:

When you need to check for many different cases on one variable you could create a large number of ElseIf statements, but this is actually very inefficient.  Without getting to specific, it takes VBScript a great deal of computing power to check all these different ElseIf statements.  Also, this kind of coding reduces human readability. Because of these problems another programming method, the Select Case statement, was created to solve this problem. The downside to this new method is you can only check if a variable is equal to something "=" and not compare it with greater than, less than, etc (>, <, >=, <=).
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

As I said above^

Limitations of Select Case:

 The Select Case statement only allows you to check if a variable is equal to a value - you can't test to see if it is greater than, less than etc. Also, you can only test against one variable. If you have multiple variables to test against, you'll need to use an If statement.
Fernando Soto

@ dstewartjr;

The link you posted was information on VBScript which is different then Visual Basic .Net 2005.

From Microsoft documentation - Select...Case Statement (Visual Basic):
Required in a Case statement. List of expression clauses representing match values for testexpression. Multiple expression clauses are separated by commas. Each clause can take one of the following forms:

expression1 To expression2
[ Is ] comparisonoperator expression

Use the To keyword to specify the boundaries of a range of match values for testexpression. The value of expression1 must be less than or equal to the value of expression2.

Use the Is keyword with a comparison operator (=, <>, <, <=, >, or >=) to specify a restriction on the match values for testexpression. If the Is keyword is not supplied, it is automatically inserted before comparisonoperator.

The form specifying only expression is treated as a special case of the Is form where comparisonoperator is the equal sign (=). This form is evaluated as testexpression = expression.

The expressions in expressionlist can be of any data type, provided they are implicitly convertible to the type of testexpression and the appropriate comparisonoperator is valid for the two types it is being used with.



This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

My bad :D)

I'll have to remember this.
Fernando Soto

Not a problem.
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Fernando Soto

@ VK;

How is the solution you posted more flexible then the one I posted?

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

It is more flexible e. g. in the case below:

Public Function Str2Lng(Text As String, Optional Default As Long) As Long
    Select Case True
        Case IsNumeric(Text)
            Str2Lng = CLng(Text)
        Case Else
            Select Case Val(Text)
                Case 0
                    Str2Lng = Default
                Case Else
                    Str2Lng = CLng(Val(Text))
            End Select
    End Select
End Function

or another case:

    Select Case True
        Case a > 0, b > 0
        Case a > 0
        Case b > 0
        Case Else
    End Select
Fernando Soto

@ VK;

Do you have a link to a technical specification that explains why one is more flexible then the other? I do not see any reason why in these example that would be so.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

It is moe flexible because of the "True" in "Select Case True".

If you have a variable instead of "true" you are restricted to
copmpare with the <<one>> variable.

With "Select Case True" you have a real replacement for the often used and bad
coding practice "elseif".

   If a Then
    End If

can be replaced with    

    Select Case True
        Case a
        Case Else
    End Select

The  "Select Case True"-constuct is a superset of the if and the elseif-statements.

Assuming vb6-language wouldn't have any if and elseif and iif-statements,
you wouldn't have any significant restrictions by using the "select case" statement.
In the inverse case it would be very inconvenient to replace the "select case"-statement.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Thanks for the help guys!  That worked perfectly.