select case Type

  • Thread starter Thread starter PJ6
  • Start date Start date
P

PJ6

Select Case o.GetType
Case = GetType(SomeRandomTypeName)
'...
End Select

Why doesn't this work? Can I make it work or am I stuck with ElseIf for a
long list?

Paul
 
Paul,
| Why doesn't this work?
Case requires "elementary data type". Type is not a "elementary", but rather
it is a class.

http://msdn2.microsoft.com/en-us/library/cy37t14y.aspx


| Can I make it work or am I stuck with ElseIf for a
| long list?
When checking for specific types, I would use a ElseIf as it ensures that
derived types are handled correctly.

You can get a select case to work, by using the type names, however then you
are limited to the type names & if you ever change the name of the type your
select case will break.

Something like:
| Select Case o.GetType.ToString
| Case "SomeNameSpace.SomeRandomTypeName"
| '...
| End Select


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Select Case o.GetType
| Case = GetType(SomeRandomTypeName)
| '...
| End Select
|
| Why doesn't this work? Can I make it work or am I stuck with ElseIf for a
| long list?
|
| Paul
|
|
 
OK, but I don't see why they have that restriction, why the "Is" operator
here cannot function the same as the "Is" operator elsewhere. It would seem
to me an arbitrary and rather unnecessary break in convention.

Paul
 
PJ6
There is no "Is" operator in a Select Case per se!

Notice that in a Select Case that "Is" is used to introduce a comparison
operator, such as <, >, <=, >=, =, or <>. That is "[Is] comparisonoperator".

IMHO: using "Case Is < 10" reads better then "Case < 10", however that is
largely personal preference.

Its similar to the "Is" keyword in the "TypeOf Is" operator.
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| OK, but I don't see why they have that restriction, why the "Is" operator
| here cannot function the same as the "Is" operator elsewhere. It would
seem
| to me an arbitrary and rather unnecessary break in convention.
|
| Paul
|
| message | > Paul,
| > | Why doesn't this work?
| > Case requires "elementary data type". Type is not a "elementary", but
| > rather
| > it is a class.
| >
| > http://msdn2.microsoft.com/en-us/library/cy37t14y.aspx
| >
| >
| > | Can I make it work or am I stuck with ElseIf for a
| > | long list?
| > When checking for specific types, I would use a ElseIf as it ensures
that
| > derived types are handled correctly.
| >
| > You can get a select case to work, by using the type names, however then
| > you
| > are limited to the type names & if you ever change the name of the type
| > your
| > select case will break.
| >
| > Something like:
| > | Select Case o.GetType.ToString
| > | Case "SomeNameSpace.SomeRandomTypeName"
| > | '...
| > | End Select
| >
| >
| > --
| > Hope this helps
| > Jay B. Harlow [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > | Select Case o.GetType
| > | Case = GetType(SomeRandomTypeName)
| > | '...
| > | End Select
| > |
| > | Why doesn't this work? Can I make it work or am I stuck with ElseIf
for
| > a
| > | long list?
| > |
| > | Paul
| > |
| > |
| >
| >
|
|
 
PJ6 said:
Select Case o.GetType
Case = GetType(SomeRandomTypeName)
'...
End Select

Why doesn't this work? Can I make it work or am I stuck with ElseIf for a
long list?


\\\
Dim t As Type = o.GetType()
Select Case True
Case t Is GetType(SomeRandomTypeName)
...
Case t Is GetType(...)
...
...
End Select
///

- or -

\\\
Select Case True
Case TypeOf t Is SomeRandomType
...
Case TypeOf t Is ...
...
...
End Select
///
 
I am using

Select case o.getType.name
Case (SomerandomTypeName)
Case ...
End Select

works as a charm since it is "o.getType.name" is a string
Roland
 
Back
Top