Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
The 'as' operator
Message
From
24/11/2003 14:59:43
 
 
To
24/11/2003 12:47:42
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00852995
Message ID:
00853041
Views:
22
Type querying is inherently slow, regardless of what kind of syntactic sugar you pour on it. The fastest 'type switch' in a polymorphic environment is calling a virtual function. If type information is lost somewhere on the way (e.g. 'sender As Object') then you have to recover at least some type information before you can call virtual functions, but at least you have to test only for one type: your base class/interface that defines the virtual function.

Be that as it may, the basic idea is
If TypeOf obj Is CFoo Then
   DirectCast(obj, CFoo).DoSomething()
End If
DirectCast is a little faster than CType because it doesn't try conversions/coercions. If you know that all obj's are actually of type CFoo or some class derived from it then you can use assertive programming:
DirectCast(obj, CFoo).DoSomething()
Previous
Reply
Map
View

Click here to load this message in the networking platform