473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

class members accessors called unexpectedly

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
Nov 17 '05 #1
6 1173

"steveevets" <st********@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
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
Nov 17 '05 #2
In message <FC**********************************@microsoft.co m>,
steveevets <st********@discussions.microsoft.com> writes
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.

--
Steve Walker
Nov 17 '05 #3
Alan Pretre <al********@newsgroup.nospam> wrote:
"steveevets" <st********@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
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.


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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
steveevets <st********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
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

"steveevets" wrote:
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

Nov 17 '05 #6
Hi

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

Thanks though.

Regards
Steve

"Alan Pretre" wrote:

"steveevets" <st********@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
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

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Jason Heyes | last post by:
Is this class evidence of poor design? class Rectangle { double width, height; public: double get_width() const { return width; } double get_height() const { return height; } void...
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
5
by: Joe Van Dyk | last post by:
Say I have the following class: using std::string; class Player { public: Player() : name(""), age(""), other_stuff("") {} private: string name; string age;
20
by: d.s. | last post by:
I've got an app with two classes, and one class (InventoryInfoClass) is an object within the other class (InventoryItem). I'm running into problems with trying to access (get/set) a private...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.