I have learned quite a bit browsing through Hidden Features of C# and was surprised when I couldn't find something similar for VB.NET.
So what are some of its hidden or lesser known features?
I have learned quite a bit browsing through Hidden Features of C# and was surprised when I couldn't find something similar for VB.NET.
So what are some of its hidden or lesser known features?
Custom Events
Though seldom useful, event handling can be heavily customized:
This can then be tested in the following fashion:
Title Case in VB.Net can be achieved by an old VB6 fxn:
Properties with parameters
I have been doing some C# programming, and discovered a feature that was missing that VB.Net had, but was not mentioned here.
An example of how to do this (as well as the c# limitation) can be seen at: Using the typical get set properties in C#... with parameters
I have excerpted the code from that answer:
Static members in methods.
For example:
In the above function, the pattern regular expression will only ever be created once no matter how many times the function is called.
Another use is to keep an instance of "random" around:
Also, this isn't the same as simply declaring it as a Shared member of the class; items declared this way are guaranteed to be thread-safe as well. It doesn't matter in this scenario since the expression will never change, but there are others where it might.
DirectCast
DirectCast
is a marvel. On the surface, it works similar to theCType
operator in that it converts an object from one type into another. However, it works by a much stricter set of rules.CType
's actual behaviour is therefore often opaque and it's not at all evident which kind of conversion is executed.DirectCast
only supports two distinct operations:Any other cast will not work (e.g. trying to unbox an
Integer
to aDouble
) and will result in a compile time/runtime error (depending on the situation and what can be detected by static type checking). I therefore useDirectCast
whenever possible, as this captures my intent best: depending on the situation, I either want to unbox a value of known type or perform an upcast. End of story.Using
CType
, on the other hand, leaves the reader of the code wondering what the programmer really intended because it resolves to all kinds of different operations, including calling user-defined code.Why is this a hidden feature? The VB team has published a guideline1 that discourages the use of
DirectCast
(even though it's actually faster!) in order to make the code more uniform. I argue that this is a bad guideline that should be reversed: Whenever possible, favourDirectCast
over the more generalCType
operator. It makes the code much clearer.CType
, on the other hand, should only be called if this is indeed intended, i.e. when a narrowingCType
operator (cf. operator overloading) should be called.1) I'm unable to come up with a link to the guideline but I've found Paul Vick's take on it (chief developer of the VB team):
(EDIT by Zack: Learn more here: How should I cast in VB.NET?)
(EDIT: Learn more here: Should I always use the AndAlso and OrElse operators?)