My preferred .NET-language is C#, but as a consultant I have to write VB every once in a while. Here is a little introduction, how my beloved
Extension Methods work in VB.Net.
Just two steps:
Step1 – Create a new Module called for example “StringExtensions.vb” and add an Import –Statement for System.Runtime.CompilerServices
Step2 – Write your Extension Method, and put an <Extension()> directive on top. Here is an example:
_ Public Function AddAbcX(ByVal mystring As String) As String Return myString + "ABC" End Function
Notice the _ behind the directive. Don’t forget this one.
You notive one additional thing. A while ago I read an article about Extension Methods and got the suggestion to add an X to each methodname.
So, if using intellisense, you will see directly if the method you’re about to call is an Extension Method or a build-in Method. Good idea I think.
Another suggestion – Put the method in the same namespace as the object it belongs to (I said namespace, not file!).
So you have your methods ready when you start using the object.
Cheers
Sascha