473,811 Members | 3,536 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 2130
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********@yah oo.comwrote in message
news:11******** **************@ b75g2000hsg.goo glegroups.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********@yah oo.comwrote in message
news:11******** **************@ b75g2000hsg.goo glegroups.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:

bSkipFirstPageI nReview = 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
5609
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 fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in a loop, they either terminate, or repeat themselves. In the mythical halting problem scenario...
11
6609
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 where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
13
1792
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 <stdlib.h> int resize(int *incsize, int min_size) { while(*incsize <= min_size) { *incsize = (int)(*incsize * 1.25); } if ( min_size > 60 ) return 0;
6
17211
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, process and output to the screen in my application when a message arrived. But the problem is how do I read the SMS message immediately when it arrived without my handphone BeEPINg for new message ? I read up the AT commands, but when getting down...
7
1988
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 declaration/directive tag - when I go to test the page it goes into an infinite loop (in debugger I've tried to break at the Page_Load method but the loop occurs before the method can even be reached)! I've rebooted the server...
53
4763
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, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating records that have a '99' in them (it is similar to a CSV file). Some of my concerns are:
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
3797
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 more why C++/CLI would be better to PInvoke than doing the PInvoke in C#? Because, usually in C# as you already know we use DLLImport and extern
5
1943
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; printf("function call: %d time \n", i); i++; main();
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10413
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9214
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3879
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3027
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.