My First Extension Method

This evening I wrote my first VB.Net extension method while working on my S#arp Architecture based project. I added another overload of the TextBox method (which is itself an extension method, I believe). I wanted to be able to specify the size of the text box. One of the existing TextBox methods has the following signature:

HtmlHelper.TextBox(name As String, value As Object, htmlAttributes As IDictionary(Of String, Object))

I wanted to be able to specify a text box size without having to declare a new IDictionary every time. So I wrote the following extension method:

<Extension()> _
Public Function(html As HtmlHelper, name As String, value As Object, size As Integer)
    Dim attributes as IDictionary(Of String, Object) = New Dictionary(Of String, Object)

    attributes.Add("size", size)

    Return html.TextBox(name, value, attributes)
End Function

Neat stuff.

Now I just have to figure out how to create an extension method that will allow me to pass a lambda expression to RedirectToAction().

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>