Properties - multiple execution of get accessor code ?

  • Thread starter Thread starter Andre Boeder
  • Start date Start date
A

Andre Boeder

In one of my test classes there is a code fragment like this:

protected System.Collections.Hashtable pData;
public System.Collections.Hashtable PData
{
get
{
//TODO: read PData from DB
System.Console.WriteLine("GET Hashtable");
return pData;
}
set { pData = value; }
}

In my test console application there is somethink like this:

Class1 test = new Class1();
System.Collections.Hashtable ht = test.PData;

Now, why do I get "GET Hashtable" 9 times !!! in the console when
debugging ? It seems that the code is really executed 9 times.

Why I tried this is I wanted to test behaviour of Properties when
using direct access like

test.PData.Add("Test", "A")

But then the whole app crashes.

Maybe you can tell me a little bit about my problem and the usage of
Properties.

Thanks,
Andre
 
Andre Boeder said:
In one of my test classes there is a code fragment like this:

protected System.Collections.Hashtable pData;
public System.Collections.Hashtable PData
{
get
{
//TODO: read PData from DB
System.Console.WriteLine("GET Hashtable");
return pData;
}
set { pData = value; }
}

In my test console application there is somethink like this:

Class1 test = new Class1();
System.Collections.Hashtable ht = test.PData;

Now, why do I get "GET Hashtable" 9 times !!! in the console when
debugging ? It seems that the code is really executed 9 times.

Why I tried this is I wanted to test behaviour of Properties when
using direct access like

test.PData.Add("Test", "A")

But then the whole app crashes.

Maybe you can tell me a little bit about my problem and the usage of
Properties.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
I would guess you have something like 'test.PData' in your watch, which
means it gets re-executed every time you step to a new line, etc. If this
is the case, remove it from your watch window, or try running without
debugging.
 
Thanks you are right - it is only a debugger problem.

But: why does

t.PData.Add("Test", "A") or something like this not work ?

If I write getters and setters by myself

t.getPData().Add("Test", "A") works fine.

Thanks very much
Andre
 
Back
Top