Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:12:42+00:00 2026-06-07T02:12:42+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:12:43+00:00Added an answer on June 7, 2026 at 2:12 am

    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() ).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem - Json.Net serializing my objects realy slow. I have some
I want to compare two times in VB.net: I have 1:42:21 PM and I
Does .NET have some type of built in function that allows filtering on Gridviews?
Does .net have the equivilent of Delphi's QuotedStr function. This replaces all quotes with
I have a datagrid in C#.net I have made some columns in grid editable.
I have an existing asp.net webforms application that I would like to add some
Hai I am developing an C# application(not asp.net) in which I have to get
During coding I frequently encounter this situation: I have several objects ( ConcreteType1 ,
I have some global components I am not sure how to put them in
Does .Net have any built in constants for common numbers like million, billion etc?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.