471,306 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

share member variable of 1 class between 2 other classes

Is there anyway to do this?

Every time any button is click inside class Form1, MyButtomArray.CharArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done the
class TEC gets executed. I want to access MyButtonArray.CharArray to get the
bool values in TEC.

Any ideas?

Scott
Jan 28 '06 #1
5 1940
Scott,
You could consider making the bool field(s) static. This means they will be
visible to any class or method in any assembly that has access to your TEC
class, without having an instance of the class, via "TEC.MyBoolFieldName". Be
aware that if you choose this route, and if your business logic dictates,
some locking semantics may be required.

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Scott Starker" wrote:
Is there anyway to do this?

Every time any button is click inside class Form1, MyButtomArray.CharArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done the
class TEC gets executed. I want to access MyButtonArray.CharArray to get the
bool values in TEC.

Any ideas?

Scott

Jan 28 '06 #2
Peter,

I created a version of static fields and it worked. But there may be two (or
more) ButtonArray's in my program I decided to make it non-static. Can I not
share a non-static variable of one class with 2 or more classes?

Scott

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:B1**********************************@microsof t.com...
Scott,
You could consider making the bool field(s) static. This means they will
be
visible to any class or method in any assembly that has access to your TEC
class, without having an instance of the class, via "TEC.MyBoolFieldName".
Be
aware that if you choose this route, and if your business logic dictates,
some locking semantics may be required.

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Scott Starker" wrote:
Is there anyway to do this?

Every time any button is click inside class Form1,
MyButtomArray.CharArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done
the
class TEC gets executed. I want to access MyButtonArray.CharArray to get
the
bool values in TEC.

Any ideas?

Scott

Jan 28 '06 #3
The only way to "share" a non-static variable between classes is by passing
a reference to the class that contains the variables to the other classes.
In other words if you want to access a method, variable or property in a
class you need to have an instance of the class or make them static.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Scott Starker" <Sc***********@sil.org> wrote in message
news:eM*************@TK2MSFTNGP12.phx.gbl...
Peter,

I created a version of static fields and it worked. But there may be two
(or more) ButtonArray's in my program I decided to make it non-static. Can
I not share a non-static variable of one class with 2 or more classes?

Scott

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:B1**********************************@microsof t.com...
Scott,
You could consider making the bool field(s) static. This means they will
be
visible to any class or method in any assembly that has access to your
TEC
class, without having an instance of the class, via
"TEC.MyBoolFieldName". Be
aware that if you choose this route, and if your business logic dictates,
some locking semantics may be required.

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Scott Starker" wrote:
Is there anyway to do this?

Every time any button is click inside class Form1,
MyButtomArray.CharArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done
the
class TEC gets executed. I want to access MyButtonArray.CharArray to get
the
bool values in TEC.

Any ideas?

Scott


Jan 28 '06 #4
If you have a parent form you can create the TEC instance there, and pass it
as a parameter in the constructor to other forms, ensuring the same instance
is used throughout. Then just expose your bools as properties of TEC. You
could also create the TEC instance in your main method and pass it in the
constructor for your Form1.

But if the ButtonArrays are getting the values from TEC after TEC "executes"
then you might consider creating an event in TEC and an EventArgs to hold
your boolean values. Then subscribe to the event in Form1. Each time TEC
fires, Form1 will have access to those boolean values.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Scott Starker" wrote:
Peter,

I created a version of static fields and it worked. But there may be two (or
more) ButtonArray's in my program I decided to make it non-static. Can I not
share a non-static variable of one class with 2 or more classes?

Scott

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:B1**********************************@microsof t.com...
Scott,
You could consider making the bool field(s) static. This means they will
be
visible to any class or method in any assembly that has access to your TEC
class, without having an instance of the class, via "TEC.MyBoolFieldName".
Be
aware that if you choose this route, and if your business logic dictates,
some locking semantics may be required.

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Scott Starker" wrote:
Is there anyway to do this?

Every time any button is click inside class Form1,
MyButtomArray.CharArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done
the
class TEC gets executed. I want to access MyButtonArray.CharArray to get
the
bool values in TEC.

Any ideas?

Scott


Jan 28 '06 #5
What are the relationships between Form1, MyButtonArray, and TEC?

For example, is MyButtonArray stored inside Form1 and so there is one
MyButtonArray for each Form1, or is it completely independent from
Form1, except that it's supposted to react to a button there? Is there
one, global TEC class, or are there several? How does the TEC class
know which MyButtonArray it's supposed to look at?

Do you see the sort of thing I'm getting at here? There are
relationships between these classes: aggregation (one class is a field
in another class), or one-to-one, or one-to-many, or many-to-many...
that sort of thing.

How classes communicate has everything to do with how they are related.
Without knowing how the classes are related, we can't tell you how to
make them communicate successfully.

Jan 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark Sisson | last post: by
6 posts views Thread by Tony Fonager | last post: by
8 posts views Thread by Tony Fonager | last post: by
7 posts views Thread by Valeriu Catina | last post: by
13 posts views Thread by Henri.Chinasque | last post: by
6 posts views Thread by Immortal Nephi | last post: by
17 posts views Thread by Juha Nieminen | last post: by

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.