My first Extension method…

Audience

Everyone

I decided as part of my .NET 3.5 learning curve to rebuild Duncan Mackenzie‘s Xbox to Twitter application just for fun…

When you call his web service you get a XboxInfo class back that contains all of your Xbox Live Information. I wanted to be able to add a method to this called “ToInstanceString” that I would use to both display your Status, and to detect when it had changed.

In VB.NET you add Extension methods to a Module. One thing worth noting is that you can control the scope of the extension method with the Namespace. If you add a namespace of “MyApp.Mynamespace” your method will only be available within that namespace and not at the “MyApp” level.

Public Module XboxExtensions

    <System.Runtime.CompilerServices.Extension()> _
    Friend Function ToPresenceString(ByVal Value As DMXIProxy.XboxInfo) As String
        If Value.PresenceInfo.Info = "" Then
            Return ""
        ElseIf Value.PresenceInfo.Info2 = "" Then
            Return Value.PresenceInfo.Info
        Else
            Return String.Format("{0} ({1})", Value.PresenceInfo.Info, Value.PresenceInfo.Info2)
        End If
    End Function

End Module

You need to annotate the method with  the “System.Runtime.CompilerServices.Extension()” attribute, and make sure that the first parameter of the method is the type that you want to extend…

You can add extension methods randomly within your code, but it makes sense to put them all together in categorised module for future use. Although this one is specific to this application, you can probably see many circumstances where you could create generic and useful methods to add to things like collections and the like…

Have fun…

 

Technorati Tags:  

Create a conversation around this article

Share on Facebook
Share on Twitter
Share on Linkdin

Read more

Martin Hinshelwood
In organizational development and team dynamics, Agile (as the Agile Manifesto delineates) and Scrum (as the Scrum Guide outlines) guide teams not by solving their problems but by illuminating the issues that demand attention. These frameworks aim to identify and spotlight the challenges within a team or organization’s processes, effectively …
Martin Hinshelwood
This week, I participated in a Scrum.org Webinar hosted by Sabrina Love (Scrum.org Product Owner) as well as my colleagues, Joanna Płaskonka, Ph.D. and Alex Ballarin to discuss the state of learning and how immersive learning is the future of training. You can watch the video below to hear what …
Martin Hinshelwood
For a long time now I have been searching for that perfect domain that epitomised the vision, the why, of what I am trying to achieve with my customers and the industry at large. Now I have found it in http://nkdagility.com
Martin Hinshelwood
At the MVP Summit I was appalled by the number of people who asked questions about new features for supporting hierarchical tasks! I shared a disgusted look with Peter Provost and we had a quick (and I mean really quick) conversation that resulted in this post. it really comes down …