Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
The 'as' operator
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00852995
Message ID:
00853016
Views:
16
Hi Claudio,

There isn't an equivalent of the "as" operator in VB.NET (unfortunately). What you need to do is check the type before you cast to it to make sure it will succeed.
Dim st As SomeType
If TypeOf obj Is SomeType Then st = CType(obj, SomeType)
If Not st Is Nothing Then ...
>Hey!
>
>what's the equivalent of C#'s "as" operator in VB.NET?
>
>in C#, we can do safe casts as in:
>
>
>human john = new Human();
>man anyman = john as man;
>if (anyman != null)
>   // john is man, so go ahead
>
>
>I know it is possible to use the "is" operator to check the type, but that's not optimized for performance. How's the optimized version for that in VB?
>
>TIA
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Reply
Map
View

Click here to load this message in the networking platform