Click here to Skip to main content
15,506,209 members
Articles / Programming Languages / Visual Basic
Posted 24 Mar 2016


1 bookmarked

An Almost Extension Property

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
27 Mar 2016CPOL2 min read
How to fake an Extension Property...


Extension methods (Function or Sub) are a powerful tool to extend an otherwise sealed object. It's easier than creating a Class extension because you do not need to update your (maybe already deployed) code, e.g. change Dim nice As SomeClass to Dim nice As SomeClassExtendedAndNicer. Obviously, different situations call for different solutions and you need an extended Class, but I prefer extension methods if I can solve the situation with those. One big huge drawback with extension methods is that you cannot write extension properties! See on MSDN here.

In this tip, I show how to (ab)use the Tag property for a proxy of an extension property or AlmostExtensionProperty. This tip was inspired by a Q&A on StackOverflow: VB extension property instead of extension method.

Code Example

First, we need a Class to function as container for some extra extension properties. This Class you would write for an ExtendedClass or an AlmostExtensionProperty alike:

Public Class ExtendedProps
    Public StrA As String = "A"
    Public DblB As Double = 2
    Public BoolC As Boolean = True
End Class

I will use a Forms Label control as an example and with an ExtendedClass you would or could code something like this:

Public Class ExtendedLabel
    Inherits Label
    Public Sub New()
        _extProps = New ExtendedProps
    End Sub
    Private _extProps As ExtendedProps = Nothing
    Public Property ExtraProperties() As ExtendedProps
            Return _extProps
        End Get
            _extProps = value
        End Set
    End Property
End Class

Nothing new so far. Now creating an AlmostExtensionProperty would call for an Extension Module and (mis)use the Tag object of the Label control like this:

Imports System.Runtime.CompilerServices
Public Module Extensions
    Public Function GetMoreProps(ByVal thisLabel As Label) As ExtendedProps
            Return CType(thisLabel.Tag,ExtendedProps)
            Return Nothing
        End Try
    End Function
    Public Function SetMoreProps(ByVal thisLabel As Label, extraProps As ExtendedProps) As Boolean
            thisLabel.Tag = extraProps
            Return True
            Return False
        End Try
    End Function
End Module

Two Extension methods which expose the ExtendedProps class through the label's Tag object is all you need. Now if you create a form with two labels on it (one regular System.Windows.Forms.Label and another special ExtendedLabel), you can test the ExtendedProps:

Public Class Main 'form
    Public Sub New()
        'Just a form with two Label controls:
        'Dim labExtendedLabelClass As New ExtendedLabel()
        'Dim labAlmostPropExt As New Label()
        labAlmostPropExt.SetMoreProps(New ExtendedProps)
        Debug.Print("Property StrA from a Label with an AlmostPropertyExtension = " & _
        Debug.Print("Property StrA from a Label extended class = " & _
    End Sub
End Class

Running this form will give you the following output:

Property StrA from a Label with an AlmostPropertyExtension = A
Property StrA from a Label extended class = A

Different method, similar result.

Points of Interest

Indeed, a probably less common use of the Tag property. However, to all accusations of improper coding, I shall answer: "Who told you the coding world is proper...?"


  • March 25th, 2016: First published


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Engineer VeeTools
Netherlands Netherlands
A (usually exploring) geologist who sometimes develops software for fun and colleagues... Check out my new website at for online mapping and coordinate conversion.

Comments and Discussions

PraiseGood idea! Pin
Mr. xieguigang 谢桂纲27-Mar-16 3:10
professionalMr. xieguigang 谢桂纲27-Mar-16 3:10 
GeneralRe: Good idea! Pin
veen_rp27-Mar-16 20:24
professionalveen_rp27-Mar-16 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.