Groups | Blog | Home
all groups > dotnet general > march 2004 >

dotnet general : How to Select Case base on type


Charles Law
3/30/2004 7:11:00 PM
Hi Gwyn

Although a bit perverse, you could reverse the test, as

Select Case True
Case TypeOf obj Is MyClass
...

Case TypeOf obj Is MyOtherClass
...

Case Else

End Select

HTH

Charles


[quoted text, click to view]

Gwyn Carwardine
3/30/2004 7:13:50 PM
Hi, often I want to do different things depending on the type of an
object...

I end up having to use:

Select Case myObj.Gettype.Name
Case Gettype(AClass).Name
.. process as AClass
Case Gettype(AnotherClass).Name
.. process as AnotherClass
End Select

Is there a better way to do this cos it strikes me as being a bit naff?!

Cheers.

Jon Skeet [C# MVP]
3/30/2004 7:20:59 PM
[quoted text, click to view]

Usually, you do it by polymorphism instead (e.g. make all the classes
implement a common interface, and call a method within that interface).
Otherwise, the above will basically work, and I don't think there's a
much better way.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Gwyn Carwardine
3/31/2004 1:12:41 PM
Very nice. Thanks a lot

g

[quoted text, click to view]

AddThis Social Bookmark Button