DotNetBlog der DotNet-Blog Alles rund um Microsoft .NET – von  Sascha H. Baumann

Extension Methods in VB.Net

1 Star2 Stars3 Stars4 Stars5 Stars (1 Bewetungen, Durchschnitt: 5.00 von 5)
Loading ... Loading ...

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:

<Extension()> _
    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

Share and Enjoy:
  • Digg
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • MisterWong.DE
  • Technorati
  • Webnews.de
  • YahooMyWeb

Hinterlasse eine Antwort

CAPTCHA-Bild CAPTCHA Audio
Bild neuladen

Bad Behavior has blocked 35 access attempts in the last 7 days.