1. Computing & Technology

Discuss in my forum

Dan Mabbutt

Thanks for the Sneaky Bug Guesses - The Answer!

By , About.com GuideAugust 24, 2009

Follow me on:

Thanks for the guesses ... Here's what happened.

It was a bug in my code. The "getter" and "setter" for a property are normally coded like this:

Public Property CatDistDefault() As Boolean
    Get
        Return _CatDistDefault
    End Get
    Set(ByVal value As Boolean)
        _CatDistDefault = value
    End Set
End Property

In this particular case, however, I coded it with just one line backwards (in the "setter"):

Public Property CatDistDefault() As Boolean
    Get
        Return _CatDistDefault
    End Get
    Set(ByVal value As Boolean)
        value = _CatDistDefault
    End Set
End Property

So, whenever the value of CatDistDefault() was returned from the class, it would always be the initialed value: False.

This was a dumb mistake on my part, but the interesting part to me was that this error was compounded by two other "features".

1 - The "getter" - "Return _CatDistDefault" - overrides even a direct assignment in the calling code. I never knew that before and that's a valuable bit of information to tuck away for future reference. In other words, when this code executes:

_AcctInfo.CatDistDefault = True

VB.NET first assigns the value True to the variable and executes the "setter" - which in my case doesn't retain the value in the object because I got it backwards. Then it immediately executes the "getter" and reassigns False to the variable again.

2 - VB.NET doesn't show the property getter and setter being executed because the default Debug option is "Step over properties and operators". So in testing this, at first I never realized that the "getter" and "setter" were even being executed. (Maybe I should have, but I didn't.) So the bottom line is that even when you single step, you won't see this code being executed. That's another thing worth remembering for future reference.

Microsoft recommends "deselecting" this option in Debug options in their documentation. (So why did they make it a default?) But Microsoft didn't include that in VB.NET Express. (They have to leave something out or why would you pay money for any other version.)

Comments
No comments yet.  Leave a Comment
Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2012 About.com. All rights reserved.

A part of The New York Times Company.