Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
The 'as' operator
Message
De
24/11/2003 14:59:43
 
 
À
24/11/2003 12:47:42
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00852995
Message ID:
00853041
Vues:
21
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()
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform