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
What to read next
My.Unity.Resolve(Of Ninja)
Shows how to create a shared UnityContainer singleton in VB.NET using the “My” namespace, enabling dependency injection across WPF and …
Calling an object method in a data trigger
Explains how to call an object method in a WPF DataTrigger, the limitations with extension methods, and common errors when binding methods …
Detecting agile theatre with real delivery signals
Why Most Companies Operating Models Fail in Dynamic Markets
A concise comparison of Predictive and Adaptive Operating Models, explaining why traditional structures fail in dynamic markets and how …
Don’t Manage Dependencies, Remove Them
Explains why dependencies are a sign of poor system design and outlines steps to eliminate them by aligning teams, clarifying ownership, and …
The Estimation Trap: How Tracking Accuracy Undermines Trust, Flow, and Value in Software Delivery
Tracking estimation accuracy in software delivery leads to mistrust, fear, and distorted behaviours. Focus on customer value, flow, and …
Flow of Value vs Flow of Work – Misnomer or Useful Shorthand?
Compares “flow of value” and “flow of work” in Kanban, explaining why only validated outcomes count as value and stressing the need for …
Why Outsourcing DevOps Fails, and How Real Engineering Excellence Starts With Your Team
Avoid DevOps vendor lock-in, discover how true engineering excellence starts with partnership, not outsourcing. Ready to transform your …