blog

My first Extension method…

Published on
2 minute read

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: .NET 

Code and Complexity Technical Excellence blog Agile Software Development Technical Excellence

Related blog posts

No related videos found.

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Capita Secure Information Solutions Ltd Logo
Akaditi Logo

NIT A/S

Brandes Investment Partners L.P. Logo
DFDS Logo
Genus Breeding Ltd Logo
Kongsberg Maritime Logo
New Signature Logo
Boxit Document Solutions Logo
SuperControl Logo
Philips Logo
Milliman Logo
Flowmaster (a Mentor Graphics Company) Logo
Higher Education Statistics Agency Logo
Bistech Logo
Qualco Logo
Jack Links Logo
Boeing Logo
Ghana Police Service Logo
Washington Department of Transport Logo
New Hampshire Supreme Court Logo
Washington Department of Enterprise Services Logo
Nottingham County Council Logo
Royal Air Force Logo
Sage Logo
YearUp.org Logo
Graham & Brown Logo

NIT A/S

Philips Logo
Deliotte Logo