468,785 Members | 1,724 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

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 6716
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Grant Richard | last post: by
reply views Thread by mailforpr | last post: by
4 posts views Thread by Frederick Gotham | last post: by
2 posts views Thread by abelniel | last post: by
3 posts views Thread by knguyen | last post: by
2 posts views Thread by Marcel Kloubert | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.