473,396 Members | 1,671 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.

How to store the reference to a "bool" ?

Hi,

In the constructor of a class, I am taking as a parameter the
reference to a "bool", as in this code:

-----------------------
public class TCP_synch_client
{
public string address ="";
public int port =0;

public TCP_synch_client(string address1,int port1,ref bool
running1)
{
address =address1;
port =port1;
// How can I store "running1" (which is a reference) in one of
the variables of the class?
}
}
-----------------------

I want to use that reference from other methods of the class. How can
I store that reference "running1" in one of the variables of the
class, and how do I declare that variable?

Is there a solution without having to change (as I suspect) "bool" by
a class or struct type? I would like to do it without having to
encapsulate my "bool" inside a class or a struct.

Thank you.

Nov 16 '05 #1
3 8535
I am curious why you are using a ref parameter in your constructor? Though
it is allowed, it is uncommon enough that I have never seen it done.

Also, I suggest not using public fields in your class.

The answer to your question, though, is identified by the additions to your
code that I have marked as being new code.

HTH

DalePres
MCAD, MCDBA, MCSE
public class TCP_synch_client
{
private bool isRunning; // This is a new line

public string address ="";
public int port =0;

public TCP_synch_client(string address1,int port1,ref bool
running1)
{
address =address1;
port =port1;

isRunning = running1; // This is a new line

// How can I store "running1" (which is a reference) in one of
the variables of the class?
}

// New property
public bool IsRunning
{
get { return isRunning; }
}

// End of New Property
}

"Mochuelo" <ho**@que.tal> wrote in message
news:ng********************************@4ax.com...
Hi,

In the constructor of a class, I am taking as a parameter the
reference to a "bool", as in this code:

-----------------------
public class TCP_synch_client
{
public string address ="";
public int port =0;

public TCP_synch_client(string address1,int port1,ref bool
running1)
{
address =address1;
port =port1;
// How can I store "running1" (which is a reference) in one of
the variables of the class?
}
}
-----------------------

I want to use that reference from other methods of the class. How can
I store that reference "running1" in one of the variables of the
class, and how do I declare that variable?

Is there a solution without having to change (as I suspect) "bool" by
a class or struct type? I would like to do it without having to
encapsulate my "bool" inside a class or a struct.

Thank you.

Nov 16 '05 #2
On Tue, 15 Mar 2005 19:18:36 -0600, "DalePres"
<don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote:
I am curious why you are using a ref parameter in your constructor? Though
it is allowed, it is uncommon enough that I have never seen it done.
In my form class, there is a variable called running,
public bool running=true;
which has to be checked and modified by most of the methods called
from there. If "true", it means everything has been ok so far, and if
"false", something has failed (for instance, a timeout has been
reached), and therefore future actions need to be skipped.

Most of the methods of class TCP_synch_client need to check and
possibly modify that "bool" variable, which is external to them. The
constructor should take a reference to that external "bool", and keep
it, so that all the other methods can use it. This has worked nicely
for years, in C++.
So, after doing the code modifications you proposed, how do I update
the value of the (external) bool variable from a method inside
TCP_synch_client which is not the constructor?

Thank you.


Also, I suggest not using public fields in your class.

The answer to your question, though, is identified by the additions to your
code that I have marked as being new code.

HTH

DalePres
MCAD, MCDBA, MCSE
public class TCP_synch_client
{
private bool isRunning; // This is a new line

public string address ="";
public int port =0;

public TCP_synch_client(string address1,int port1,ref bool
running1)
{
address =address1;
port =port1;

isRunning = running1; // This is a new line

// How can I store "running1" (which is a reference) in one of
the variables of the class?
}

// New property
public bool IsRunning
{
get { return isRunning; }
}

// End of New Property
}


Nov 16 '05 #3
Add a setter to the property:

set { isRunning = value; }

Then your class consumers will do something like this:

// Notice no ref parameter!
TCP_synch_client tcp = new TCP_synch_client(address, port, running);
running = tcp.IsRunning;

// code that changes running

tcp.IsRunning = running.
Or, since the running state is now available as a property of your
TCP_synch_client, you don't even have to use your running variable now.
Just use the property directly. But if your application has the variable
running used extensively you could continue to use it as I have shown above.

DalePres
"Mochuelo" <ho**@que.tal> wrote in message
news:6f********************************@4ax.com...
On Tue, 15 Mar 2005 19:18:36 -0600, "DalePres"
<don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote:
I am curious why you are using a ref parameter in your constructor?
Though
it is allowed, it is uncommon enough that I have never seen it done.


In my form class, there is a variable called running,
public bool running=true;
which has to be checked and modified by most of the methods called
from there. If "true", it means everything has been ok so far, and if
"false", something has failed (for instance, a timeout has been
reached), and therefore future actions need to be skipped.

Most of the methods of class TCP_synch_client need to check and
possibly modify that "bool" variable, which is external to them. The
constructor should take a reference to that external "bool", and keep
it, so that all the other methods can use it. This has worked nicely
for years, in C++.
So, after doing the code modifications you proposed, how do I update
the value of the (external) bool variable from a method inside
TCP_synch_client which is not the constructor?

Thank you.


Also, I suggest not using public fields in your class.

The answer to your question, though, is identified by the additions to
your
code that I have marked as being new code.

HTH

DalePres
MCAD, MCDBA, MCSE
public class TCP_synch_client
{
private bool isRunning; // This is a new line

public string address ="";
public int port =0;

public TCP_synch_client(string address1,int port1,ref bool
running1)
{
address =address1;
port =port1;

isRunning = running1; // This is a new line

// How can I store "running1" (which is a reference) in one of
the variables of the class?
}

// New property
public bool IsRunning
{
get { return isRunning; }
}

// End of New Property
}

Nov 16 '05 #4

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

Similar topics

6
by: kelvSYC | last post by:
This little bit of seeminly innocent code seems to give me these two errors, all on the line that declares check(). Is there some part of C++ that I'm missing out on? class Condition { public:...
8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
2
by: Chris Wood | last post by:
In C#, I am calling a method implemented in Managed C++ that returns an array of booleans. This method in turn calls unto unmanaged C++ code that returns an unsigned byte array, which is...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
4
by: Frederick Gotham | last post by:
Does "bool" behave _exactly_ like any other integer type, except for when it comes to integer promotion? Consider the following C function whose purpose it is to calculate the quantity of equal...
2
by: abelniel | last post by:
Hi all, When I use "bool" type in my program, gcc pop an error error: `bool' undeclared (first use in this function) I have included the following lib: #include <stdio.h> #include...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
3
by: knguyen | last post by:
Hello, I was trying to compile the source code from "Design Patterns: Elements of Reusable Object-Oriented Software", which can be obtained from here...
2
by: Marcel Kloubert | last post by:
Hi! Example: public bool? IsChecked { get { throw new NotImplementedException(); } } What does the question mark after the bool mean ("bool?")?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.