How to Select Case base on type

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

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
 
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.
 
Gwyn Carwardine said:
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?!

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.
 
Very nice. Thanks a lot

g

Charles Law said:
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
 
Back
Top