Power11: Extension properties

[This post is part of a series, "wish-list for future versions of VB"]


IDEA: Extension properties. We should add extension properties, just like we have extension methods.


SCENARIO: In Javascript and a host of web-related DSLs, there are many attempts at a easy syntax for constructing HTML elements, but none of them really pull it off succesfully. VB with its XML literals could do a better job, especially with extension properties:

    doc.getElementById("div").innerNodes = <ul><li>Hello</li><li>World</li></ul>



    WriteOnly Property innerNodes(ByVal x As HtmlElement) As XElement

        Set(ByVal value As XElement)

            x.Add(... value ... need to do some munging here)

        End Set

    End Property


On Internet Explorer, people sometimes use ".innerHTML" instead and then ask the browser to parse this text string. But this doesn't work on Firefox, and isn't as fast as creating the nodes directly.


SCENARIO: User "Eidolon II" suggests, for the Session object in an ASP.NET project, using extension properties to wrap session variables. Currently it needs to be done by adding an extension Sub getter and extension function setter accessors.


    Public Property SelectedProductId(ByVal target As HttpSessionState) As Integer


            Return target("selprodid")

        End Get

        Set(ByVal value As Integer)

            target("selprodid") = value

        End Set

    End Property


Did you know that VB already has extension properties? More precisely, it has three extension properties for working with XML:

        Dim x = <xml><a>hello</a></xml>

        Dim y = x.<a>.Value

Here x.<a> returns an IEnumerable(Of XElement), and "Value" is an extension property on that. It's defined inside InternalXMLHelper, a class that we spit out into any user assembly that uses XML literals. We didn't extend extension-properties to a full user-usable feature because that would have involved a lot more design work, and testing, and we ran into places where extension properties wouldn't even do what we wanted them to.


Provisional evaluation from VB team: This is a decent idea, one worth considering against the other decent ideas. If you see more scenarios where extension properties would help you, please write about them! Or if they wouldn't help your particular scenarios, then please write in about those as well.

Comments (3)

  1. Andy says:

    There is one senario where extension properties would be helpful for me.  Currently; I have extensions for IEnumerable because I have to use IEnumerable object to use Covariance and Contravariance easily.  Would be nice to Extend it for the Index (since List has that).  Would be something like this


    Property Item(Of T)(ByRef ThisList as IEnumerable(Of T), ByVal Index as Integer) As T


    Dim TempList=ThisList.ToList

    return TempList.Item(Index)

    End Get

    Set(ByVal ThisItem as T)

    dim TempList=ThisList.ToList



    End Set

  2. Kevin Ryall says:

    This is an obvious addition that would the language just that little bit more flexible. Any syntactic sugar you can add that makes properties real first class members instead of thinly disguised wrappers around accessor / mutator methods is all to the good.

    This is a lack I have felt recently when using extension methods with interfaces to make implementation less onerous (pseudo-mixins I guess), but being able to use extension properties would have made this much ‘cleaner’ – I don’t like to use accessor / mutator methods just because there are no extension properties.

  3. weitzhandler says:

    This is sumething that should have been released along with ex. methods.

    BTW, why would u wanna name a property (or method) name as lower camel case

Skip to main content