How to store the reference to a "bool" ?

M

Mochuelo

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

DalePres

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
}
 
M

Mochuelo

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

DalePres

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
 

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