a·gen·tic a·gil·i·ty

My first Extension method...

TL;DR; Explains how to create and use extension methods in VB.NET, with an example for enhancing the XboxInfo class to display and track Xbox Live status changes.

Published on
2 minute read
Image
https://nkdagility.com/resources/QJ6lF5ONMCD
Subscribe

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.

 1Public Module XboxExtensions
 2
 3    <System.Runtime.CompilerServices.Extension()> _
 4    Friend Function ToPresenceString(ByVal Value As DMXIProxy.XboxInfo) As String
 5        If Value.PresenceInfo.Info = "" Then
 6            Return ""
 7        ElseIf Value.PresenceInfo.Info2 = "" Then
 8            Return Value.PresenceInfo.Info
 9        Else
10            Return String.Format("{0} ({1})", Value.PresenceInfo.Info, Value.PresenceInfo.Info2)
11        End If
12    End Function
13
14End 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

Subscribe

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.​

Kongsberg Maritime Logo

Kongsberg Maritime

Brandes Investment Partners L.P. Logo

Brandes Investment Partners L.P.

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

Lean SA Logo

Lean SA

NIT A/S

SuperControl Logo

SuperControl

Ericson Logo

Ericson

MacDonald Humfrey (Automation) Ltd. Logo

MacDonald Humfrey (Automation) Ltd.

Qualco Logo

Qualco

Teleplan Logo

Teleplan

Bistech Logo

Bistech

Emerson Process Management Logo

Emerson Process Management

Hubtel Ghana Logo

Hubtel Ghana

Jack Links Logo

Jack Links

Schlumberger Logo

Schlumberger

Workday Logo

Workday

ALS Life Sciences Logo

ALS Life Sciences

YearUp.org Logo

YearUp.org

Royal Air Force Logo

Royal Air Force

Nottingham County Council Logo

Nottingham County Council

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Ghana Police Service Logo

Ghana Police Service

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Washington Department of Transport Logo

Washington Department of Transport

Trayport Logo

Trayport

ProgramUtvikling Logo

ProgramUtvikling

Epic Games Logo

Epic Games

Alignment Healthcare Logo

Alignment Healthcare

Brandes Investment Partners L.P. Logo

Brandes Investment Partners L.P.

Schlumberger Logo

Schlumberger