Click here to Skip to main content
15,113,873 members
Articles / Programming Languages / C#
Tip/Trick
Posted 25 Apr 2021

Tagged as

Stats

2.2K views
15 downloads
2 bookmarked

Little Elves Property Management

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
25 Apr 2021CPOL5 min read
How Little Elves mess with your code
Explains how Little Elves managing your properties can meddle more than Scooby Doo and the Gang

Image 1

Introduction

Most people believe that Properties are an extension to Visual Studio's Variable class that have get & set accessors, but as you must have learned by now, Visual Studio Properties are something surprisingly different. It's obvious once you know but here's how you can prove it for yourself: If you ask a variable what it's value is, it will do no more than tell you the content of its memory but if you ask a Property what its value is, it will execute code for you whenever you interface with it, thus proving that Properties really are Little Elves living inside your computer.

They, of course, can be very handy.

What can happen though, when you write your property's get or set accessors, you can tell it to create content for itself before reporting what that content is, thereby altering the content of some of your variables every time you ask it what it's value is. And again, this is great. That's what they're for.

But what might surprise you is that the code you write in their get/set accessors can be executed even while you're at a break point, debugging your code, without even stepping through any of it. When you're debugging and the program flow has stopped. So you would, therefore, not expect your variables to change without you stepping through any lines of your code ... but they might.

Watch a short video about this article.

You'd think this could be a case for ScoobyDoo, and you'd be right, but Velma will be the first to tell you there's no ghost here ... like all things ScoobyDoo it's probably just another greedy real-estate agent trying to pull a scam on you (except for the Easter Bunny ... he's real). And, to confuse the matter further, your properties are actually being manipulated without your saying-so by Little Elves who sit at their stations clutching two mission-plans waiting to know which plan you want them to execute next. But these little Keeblers don't ever take a break. I mean they won't let up ... its a little creepy. They're always there executing code, like you told them to, except they sometimes get so excited, they forget to log their work or even tell you about it.

Supposing, let's say, you had the variable, the property and the method that are shown here:

C#
Bitmap bmp_Variable = null;

Bitmap bmp_Property
{
    get 
    {
        bmp_Method();
        return bmp_Variable;
    } 
    set {  }  
}

void bmp_Method()
{
    if (bmp_Variable == null)
        bmp_Variable = new Bitmap(25, 25);
    System.IO.File.WriteAllText(System.IO.Directory.GetCurrentDirectory() 
                                            + "\\bmp_Method.txt", 
                                "this file was created at " 
                                            + DateTime.Now.ToString() 
                                            + " by bmp_Method()");
}

and one wonderful Little-Elf sitting at her desk with a printed card in each hand managing your property.

The card in her left hand has the property's set accessor instructions written on it in her stylish cursive script and in her right hand she has the property's get accessor instructions neatly printed there. So, there she sits waiting for you, like she's a well rehearsed percussionist in the metropolitan symphonic orchestra holding a tiny little triangle in her slender uplifted hand and you're standing there on a dais with the baton in your hand, maestro for the whole show.

And that's great. That's what you want Little Elves like her to do. But, when you're debugging your work at a break point in your code. The program flow has stopped. None of your methods or events are being called because you're debugging ... in terms of code-execution ...

Time has Stopped.

so you'd expect your variables to remain the same.

But that pretty Little Elf keeps busy, busy, busy. And never goes away from her desk. So, when you write her name up on the board (in the IDE's Watch Window), she gets all excited. Pees a little. And then looks at the property's get accessor instructions written on the card in her right hand. Then does exactly what you told her to do when you hired her in the first place and a set her to work with these two plans to help her along.

Now, that's what you want from your squad of property managing Little Elves. Quick, efficient work.

The problem though is that even if you put a Break-Point in the property's accessor and even if your program flow has stopped dead in its tracks while you're debugging. Even though you haven't stepped-through a single line of code since you've stopped at a break-point, your over-zealous Little Elf will execute the instructions written on the card in her hand and never log the fact that it redesigned your corner-office and ruined your entire Feng Shui. So, while you're working from home and haven't noticed a thing wrong with your office, you do see that your papers are as fudged as Keebler thumbs and your figures aren't quite right.

So you call Scooby and the gang! ... and this time, ScoobyDoo might be the one to stumble on the mysterious goings on with your properties and their better management but its Daphne who will see that your problem isn't an unscrupulous real-estate developer trying to turn a profit on your property but rather one over-zealous Little Elf who went ahead and did her job, just like you told her to, even though the lunch whistle blew twenty minutes ago. And, she doesn't want to bother HR with the paper-work while they're on lunch, so she won't log any of her work with them at all until after her co-workers march past her gingerbread desk and go back to work when you quit debugging and resume your program's execution.

Supposing you were at a break point debugging the code we just looked at earlier. If you go to the Watch Window in the IDE and type the name of the variable there, you'll see that the variable was initialized to null and hasn't changed since the app started but if you type the name of the property and press Enter, that same variable will have its value altered by your zealous Little Elf.

Even if

  • your code never once includes a single reference to that property
  • you put a Break-Point in the property's accessor to see it happen
  • your program is not running while you're debugging

Image 2

So, you see, Little Elves truly are wonderful but you do have to keep an eye on them or their zealous ways may fudge things up some for you.

History

  • 25th April, 2021 - Originally published

License

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

Share

About the Author

Christ Kennedy
CEO unemployable
Canada Canada
Christ Kennedy grew up in the suburbs of Montreal and is a bilingual Quebecois with a bachelor’s degree in computer engineering from McGill University. He is unemployable and currently living in Moncton, N.B. writing his next novel.

Comments and Discussions

 
SuggestionI never do setting a variable in a getter Pin
gvdpeer26-Apr-21 21:12
Membergvdpeer26-Apr-21 21:12 
AnswerRe: I never do setting a variable in a getter Pin
Daniele Rota Nodari26-Apr-21 23:26
MemberDaniele Rota Nodari26-Apr-21 23:26 
GeneralRe: I never do setting a variable in a getter Pin
Christ Kennedy29-Apr-21 7:55
mvaChrist Kennedy29-Apr-21 7:55 
thanks for those examples,
property's can be great tools for those exact reasons, just gotta keep the zealous Little Elves in mind when debugging.
my code is perfect until i don't find a bug...

GeneralRe: I never do setting a variable in a getter Pin
Christ Kennedy29-Apr-21 7:57
mvaChrist Kennedy29-Apr-21 7:57 
SuggestionDebuggerBrowsableAttribute Pin
Daniele Rota Nodari25-Apr-21 22:19
MemberDaniele Rota Nodari25-Apr-21 22:19 
GeneralRe: DebuggerBrowsableAttribute Pin
Christ Kennedy26-Apr-21 1:40
mvaChrist Kennedy26-Apr-21 1:40 

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.