Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Case with "Is" operator

In VB.NET, I have to compare some objects within a select case statement.

Since select case uses = operator by default and this is not defined for objects, a compile error is thrown.

I presently use this workaround:

Select Case True
    Case sender Is StyleBoldButton

    Case sender Is StyleUnderButton

    Case sender Is StyleItalicButton

End Select

which actually works.

Is there something prettier to see and more understandable?

like image 995
Teejay Avatar asked Jul 03 '12 10:07

Teejay


People also ask

Is select case a conditional statement?

Select Case is a conditional statement, that helps you test a variable for equality against a set of values. Each value is referred to as a case, and a variable that is being switched on should be checked for all the select cases.

What is select case write its code?

A Select Case statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each select case.

Why is a Select case used instead of an if statement?

The SELECT CASE statement allows you to compare an expression to multiple values. It is similar to the IF THEN ELSE statement except that the IF THEN ELSE statement can evaluate multiple expressions. The SELECT CASE statement evaluates one expression.

Which part of the Select Case statement is optional?

The CASE statement always goes in the SELECT clause. CASE must include the following components: WHEN , THEN , and END . ELSE is an optional component. You can make any conditional statement using any conditional operator (like WHERE ) between WHEN and THEN .


2 Answers

Anything that has the requisite comparison operators (=, >=, <=, etc.) defined is fair game for Select Case. Rightly (or wrongly), references just aren't compared with = in VB; one must use Is. (Or Object.Equals(objA As Object, objB As Object) - but, really, why? when you've got Is?)

But take a look at Object equality behaves different in .NET - perhaps the VB way is less confusing? Whatever, I think you're stuck with the If-ElseIf ladder since Select Case doesn't do Is. (well, it does, but that's a different Is, more like the it of Hypercard.) I think the ladder looks smart and easy to follow:

If sender Is StyleBoldButton Then 

ElseIf sender Is StyleUnderButton Then

ElseIf sender Is StyleItalicButton Then

Else

End If 

As you have pointed out, the Select Case True pattern is an "OrElse" short-circuit workaround in VB6 - a wonky way to meet a real need. But that's not needed in VB.NET. In that spirit, maybe it's better to use design patterns more in line with the best practices expected of an object-oriented language. For example, as Denis Troller suggested, why not give each button its own event handler?

But if you insist on something like an Is-able Select, here's something I probably won't use myself:

With sender
    If .Equals(StyleBoldButton) Then

    ElseIf .Equals(StyleUnderButton) Then

    ElseIf .Equals(StyleItalicButton) Then

    Else

    End If
End With

Here I'm counting on .Equals to work like the C# == when faced with two object types to compare (see http://visualstudiomagazine.com/articles/2011/02/01/equality-in-net.aspx). The beauty of this is that sender is mentioned only once; however there's all this ElseIf .Equals( ... ) Then you'll have to type for each "Case".

Another way I won't use myself is using GetHashCode():

Select Case sender.GetHashCode()

    Case StyleBoldButton.GetHashCode()

    Case StyleUnderButton.GetHashCode()

    Case StyleItalicButton.GetHashCode()

    Case Else

End Select

Here I'm counting on what (very) little I know of GetHashCode() to uniquely (enough) identify these controls. (See Default implementation for Object.GetHashCode() ).

like image 158
rskar Avatar answered Oct 09 '22 02:10

rskar


I just came across this same problem. After seeing another post and this post I came to this solution for myself and I wanted to share in case someone out there really wanted to use the Select Case like I did :)

    Select Case DirectCast(sender, Button).Name
        Case StyleBoldButton.Name

        Case StyleUnderButton.Name

        Case StyleItalicButton.Name

    End Select

Update 6-16-16: Removed "Is = " because it was unnecessary.

Update 8-27-16: Changed the use of strings to use .Name for better error tracking.

like image 37
Jeffrey Avatar answered Oct 09 '22 04:10

Jeffrey