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
        End Select

Open in new window

Visual Basic.NETVisual Basic ClassicRegular Expressions

Avatar of undefined
Last Comment
freebullets
Avatar of Don
Don
Flag of United States of America image

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 (>, <, >=, <=).
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Don
Don
Flag of United States of America image

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.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

@ 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):
expressionlist
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
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.

http://msdn.microsoft.com/en-us/library/cy37t14y.aspx

Fernando




Avatar of Don
Don
Flag of United States of America image

My bad :D)

I'll have to remember this.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Not a problem.
SOLUTION
Avatar of VK
VK
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

@ VK;

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

Fernando
Avatar of VK
VK
Flag of Germany image

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
Avatar of VK
VK
Flag of Germany image

or another case:

    Select Case True
        Case a > 0, b > 0
        Case a > 0
        Case b > 0
        Case Else
    End Select
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

@ 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.
Avatar of VK
VK
Flag of Germany image

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".
Avatar of VK
VK
Flag of Germany image

   If a Then
    Else
    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.
Avatar of VK
VK
Flag of Germany image

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.
Avatar of freebullets
freebullets

ASKER

Thanks for the help guys!  That worked perfectly.
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo