Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Case on an object's type in VB.NET

I'm not sure if this valid C#, but hopefully you get the idea. :)

switch (msg.GetType()) {     case ClassA:         // blah     case ClassB:         // blah 2     case ClassC:         // blah 3 } 

How would I switch on an object's type but using VB.NET's Select Case?

I'm aware that some might suggest using polymorphism, but I'm using a hierarchy of small message classes so that really wouldn't work in my case.

like image 456
mcjabberz Avatar asked Aug 19 '09 18:08

mcjabberz


People also ask

What is the syntax for select case?

Select Case statements can be nested. Each Select Case statement must have a matching End Select statement. The expression list that follows the Case in a Select Case statement must be a list of constant numeric or string expressions. If you want to use variables for different cases, use the If...Then...

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.

How do you write switch statement in VB?

To execute a group of statements depending upon the value of an Expression, then we use the Switch Case. Here, each value is called a Case, and the variable is being switched ON based on each case. Else statement case is executed if the test expression doesn't match with any of the Case specified by the user.

What is Select Case statement in computer?

A SELECT CASE statement, often referred to as a CASE statement, is used to compare a given value with preselected constants and take an action according to the first constant to match. A CASE statement can be handy to select between a series of different possibilities or cases.


2 Answers

With VB 2010, for projects targeting .NET framework 4 and later, you can now do this:

Select Case msg.GetType()     Case GetType(ClassA) End Select 

In earlier VB versions, it didn't work because you couldn't compare two types with equality. You'd have to check if they point to the same reference using the Is keyword. It's not possible to do this in a Select Case, unless you use a property of the type like the Name or FullName for comparison, as suggested by Michael. You can use a combination of If and ElseIf though:

Dim type = msg.GetType() If type Is GetType(ClassA)     ... ElseIf type Is GetType(ClassB)     ... ... End If 
like image 152
Meta-Knight Avatar answered Sep 24 '22 23:09

Meta-Knight


Well, if you insist on using Select Case, you could always go with:

Select Case True     Case TypeOf msg Is ClassA         ' do something '     Case TypeOf msg Is ClassB         ' do something else '     Case Else         ' and so on ' End Select 

But I would imagine most people like to avoid this kind of thing. If/ElseIf would probably be clearer.

like image 20
Dan Tao Avatar answered Sep 25 '22 23:09

Dan Tao