Mark Gilbert's Blog

Science and technology, served light and fluffy.

Be kind to your grey matter: Class Properties

There are a couple of patterns that I’ve frequently used in the past to simplify getting raw data from a query string parameter, form, or session variable or setting data on a form or session variable.  These two patterns have the same basic approach – encapsulate the processing logic in a property.

For the former, the pattern would work something like this:

 

Private ReadOnly Property Quantity() As Integer

    Get

        If(IsNothing(Request.QueryString(“quantity”))) Then Return 0

        If(Not IsNumeric(Request.QueryString(“quantity”))) Then Return 0

        Return CType(Request.QueryString(“quantity”), Integer)

    End Get

End Property

 

As you can see, I can now simply say “Me.Quantity” in code, and all of the validation and “cleanup” is done for me in the Property getter.

The second pattern involves wrapping a form field in a property:

 

Private Property Quantity() As Integer

    Set(ByVal value As Integer)

        Me.QuantityField.Text = value.ToString.Trim

    End Set

    Get

        If(Me.QuantityField.Text.Trim.Length = 0) Then Return 0

        If(Not IsNumeric(Me.QuantityField.Text)) Then Return 0

        Return CType(Me.Quantity.Text, Integer)

    End Get

End Property

 

Again, the logic is contained in the Property, and all I have to say in code is “Me.Quantity”.

In both of these cases, the grey matter upstairs only has to think about “something = Quantity” or “Quantity = something”, and not have to remember to clean the data up each time a session object is reference, or a form field is set.

I hit a case late last week where I needed both patterns for the same logical piece of data (quantity, in this case).  The page would accept the quantity on the query string, and would store it in a field on the form.  My first thought was to handle this with a single property, like the following:

 

Private Property Quantity() As Integer

    Set(ByVal value As Integer)

        Me.QuantityField.Text = value.ToString.Trim

    End Set

    Get

        If(IsNothing(Request.QueryString(“quantity”))) Then Return 0

        If(Not IsNumeric(Request.QueryString(“quantity”))) Then Return 0

        Return CType(Request.QueryString(“quantity”), Integer)

    End Get

End Property

 

Which would have meant that I would have used something like this in code:

 

Me.Quantity = Me.Quantity

 

Ouch!  Ok, grey matter is twitching!

It didn’t take me long to decide that that would be WAY too confusing for me to hit 6 months from now when I’m trying to maintain this page, let alone for another, unsuspecting, developer.  I came up with Plan B.  I created two properties: one called “DefaultQuantity” which was ReadOnly and would handle grabbing the value off of the query string; the other called “SpecifiedQuantity” which would wrap the form field.  Then my assignment looked like this:

 

Me.SpecifiedQuantity = Me.DefaultQuantity

 

Ah.  Much better.  Grey matter happy now.

Advertisements

September 30, 2008 - Posted by | Visual Studio/.NET

Sorry, the comment form is closed at this time.

%d bloggers like this: