class members accessors called unexpectedly

G

Guest

Hi

I have written a class with a private member & public member with accessors
thus

public class Wierd
{
private int _MyInt = 0;
private bool _MyIntGot = false;
public int MyInt
{
get
{
_MyIntGot = true;
return _MyInt;
}
set
{
_MyInt = value;
_MyIntGot = true;
}
}


public Wierd()
{

}
}

then on a windows form i have

private void button1_Click(object sender, System.EventArgs e)
{
Wierd _MyWierd = new Wierd();
string g = "g";
g = "hh"; // do something
g = "kk"; // do something
int i = _MyWierd.MyInt;
}

In the debugger I can see that _MyWierd._MyIntGot becomes true before the line
int i = _MyWierd.MyInt;

The get accessor for _MyWierd.MyInt seems to be being called, though no
breakpoint is being hit there.

Can anyone tell me what is going on here?

Thanks in advance

Steve
 
A

Alan Pretre

steveevets said:
public class Wierd
{
private int _MyInt = 0;
private bool _MyIntGot = false;
}

It's because you are using static initialization of _MyInt and _MyIntGot.
Move their initialization to the constructor and it should be fine.

public class Wierd {
private int _MyInt;
private bool _MyIntGot;

public int MyInt {
get {
_MyIntGot = true;
return _MyInt;
}
set {
_MyInt = value;
_MyIntGot = true;
}
}


public Wierd() {
_MyInt = 0;
_MyIntGot = false;
}
}



-- Alan
 
S

Steve Walker

In message said:
Hi

I have written a class with a private member & public member with accessors
thus

In the debugger I can see that _MyWierd._MyIntGot becomes true before the line
int i = _MyWierd.MyInt;

The get accessor for _MyWierd.MyInt seems to be being called, though no
breakpoint is being hit there.

Can anyone tell me what is going on here?

Every time you view the value of the property, the code in the property
is being run.

Try this:

class Test
{
int i=0;
public int I
{
get{return i++;}
}
}

Instantiate it, set a breakpoint, and hover over some code using the
property. It goes up, rapidly.
 
J

Jon Skeet [C# MVP]

Alan Pretre said:
It's because you are using static initialization of _MyInt and _MyIntGot.
Move their initialization to the constructor and it should be fine.

No it's not - that's not static intiialization, and it won't invoke the
properties.

My guess is that it's using the debugger that's causing the
behaviour...
 
J

Jon Skeet [C# MVP]

steveevets said:
I have written a class with a private member & public member with accessors
thus

<snip>

Are you by any chance watching this in a debugger? It may be executing
the property for you in order to show you the value. If that's not the
case, 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.
 
G

Guest

Hi
Yes it's the debugger that causes the effect.
Microsoft do (I have now read) specifically warn against modifing an object
within a get. I am guilty here of "Bad programming practice" & won't do it
again!

Thanks for your response.

Regards
Steve
 
G

Guest

Hi

No it's the debugger accessing the get inorder to show the value, & cleverly
bypassing the breakpoint.

Thanks though.

Regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top