473,499 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using property gets me into infinite loop

Hi,

I have a C# class that contains a property defined as follows:

public bool bRunTwice
{
get { return bRunTwice; }
set { bRunTwice = value; }
}

Then I have a method in the same class that uses the property as
below:

bRunTwice = true;

This causes an infinite loop. However, if I replaced the property
block to a public member (which is not the best way):

public bool bRunTwice;

Everything works. How come using property causes trouble?

Mar 15 '07 #1
7 2111
VJ
your property variable and and Property name are same..

How did the first complie for you? It won't work, same variable name in
class !!

VJ

"Curious" <fi********@yahoo.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
Hi,

I have a C# class that contains a property defined as follows:

public bool bRunTwice
{
get { return bRunTwice; }
set { bRunTwice = value; }
}

Then I have a method in the same class that uses the property as
below:

bRunTwice = true;

This causes an infinite loop. However, if I replaced the property
block to a public member (which is not the best way):

public bool bRunTwice;

Everything works. How come using property causes trouble?

Mar 15 '07 #2
VJ
Oh yes.. I just ran a test... you can just do the below..without declaring a
private variable bRunTwice.
public bool bRunTwice
{
get { return bRunTwice; }
set { bRunTwice = value; }
}

It complies... yicky... :-)

Anyway curious.. please keep the private variable and Property name
different.

VJ

"Curious" <fi********@yahoo.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
Hi,

I have a C# class that contains a property defined as follows:

public bool bRunTwice
{
get { return bRunTwice; }
set { bRunTwice = value; }
}

Then I have a method in the same class that uses the property as
below:

bRunTwice = true;

This causes an infinite loop. However, if I replaced the property
block to a public member (which is not the best way):

public bool bRunTwice;

Everything works. How come using property causes trouble?

Mar 15 '07 #3
On Mar 15, 12:27 pm, "VJ" <nonewsaddr...@yahoo.comwrote:
your property variable and and Property name are same..

How did the first complie for you? It won't work, same variable name in
class !!

VJ
Since it's a property, it's not a private member. I just use it like
follows:

bSkipFirstPageInReview = true; //This calls set { bRunTwice =
value; } infinite number of times. Why?
It compiles.

Mar 15 '07 #4
Since it's a public property, it's not a private member. I use it the
way follows:

bRunTwice = true; //This calls set { bRunTwice =
value; } infinite number of times. Why?

BTW, it compiles fine.

Mar 15 '07 #5
Curious wrote:
On Mar 15, 12:27 pm, "VJ" <nonewsaddr...@yahoo.comwrote:
your property variable and and Property name are same..

How did the first complie for you? It won't work, same variable name in
class !!

VJ

Since it's a property, it's not a private member.
Whether a member is a property or not is orthogonal to its visibility.
Private properties are perfectly valid.
I just use it like
follows:
Properties don't have any storage. They are simply syntactic sugar for
one or two methods. Your definition in your original post goes into an
infinite loop because it recursively calls the getter in the getter, and
the setter in the setter.

The standard pattern for most properties is to have a private field on
the class, and the property reads from and writes to the field. E.g.:

class T
{
int _myField;

int MyField
{
get { return _myField; }
set { _myField = value; }
}
}

-- Barry

--
http://barrkel.blogspot.com/
Mar 15 '07 #6
Hi Barry,

What you suggest sounds right to me. Let me try it and let you know.
Many thanks!

Mar 15 '07 #7
Hi Barry,

Thanks for the advice. It works!

Mar 15 '07 #8

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

Similar topics

43
5525
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
11
6548
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
13
1760
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include...
6
17133
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
7
1970
by: Kewlpack | last post by:
Okay - this is stumping me. I've used .Net since 1.0 release and never had this trouble before... In one of my new projects, if I enable the SmartNavigation="True" in the Page...
53
4595
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
44
4320
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
14
3766
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
5
1927
by: Tinku | last post by:
I am sorry for asking this silly question, but i don't understand why it is happening please suggest me ================================= #include <stdio.h> int main() { static int i=1;...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
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
7180
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
7229
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...
1
6905
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
5485
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,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.