473,728 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.C harArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done the
class TEC gets executed. I want to access MyButtonArray.C harArray to get the
bool values in TEC.

Any ideas?

Scott
Jan 28 '06 #1
5 2055
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.MyBoolFiel dName". 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.C harArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done the
class TEC gets executed. I want to access MyButtonArray.C harArray 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*******@yaho o.nospammin.com > wrote in message
news:B1******** *************** ***********@mic rosoft.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.MyBoolFiel dName".
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.C harArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done
the
class TEC gets executed. I want to access MyButtonArray.C harArray 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******** *****@TK2MSFTNG P12.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*******@yaho o.nospammin.com > wrote in message
news:B1******** *************** ***********@mic rosoft.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.MyBoolFiel dName". 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.C harArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done
the
class TEC gets executed. I want to access MyButtonArray.C harArray 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*******@yaho o.nospammin.com > wrote in message
news:B1******** *************** ***********@mic rosoft.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.MyBoolFiel dName".
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.C harArray
(MyButtomArray is a class) gets set (or reset) (bool). Once this is done
the
class TEC gets executed. I want to access MyButtonArray.C harArray 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7847
by: Mark Sisson | last post by:
Hi all. SITUATION ================ 1. I have a base class with a member variable that's an object 2. I have several classes that inherit from the base class. 3. There are several methods in the base class that modify the member variable 4. I would like to have the inherited classes override the member variable with a new var that's a subclass of the parent's member variable
6
1619
by: Tony Fonager | last post by:
I am currently developing a statistics system in ASP.NET, and need to share information about the customers websites, in this application. (I have simplified my code, to make my project easier to explain.) The simple version of the system is like this : A customer inserts HTML code on his webpage, which contacts my statistics server each time the customers website recieves a hit - like a classic "register website traffic" system. ...
8
4554
by: Tony Fonager | last post by:
I am currently developing a statistics system in ASP.NET, and need to share information about the customers websites, in this application. (I have simplified my code, to make my project easier to explain.) The simple version of the system is like this : A customer inserts HTML code on his webpage, which contacts my statistics server each time the customers website recieves a hit - like a classic "register website traffic" system. ...
7
2114
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
7
2183
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
15
3848
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can* populate the variable, but the population is not *required* by the derived class (which must be the case). What would meet the requirements is if I create an abstract method in the base class that populates the member variable. In this case the...
13
11471
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
6
2180
by: Immortal Nephi | last post by:
First class is the base class. It has two data: m_Base1 and m_Base2. Second class and third class are derived classes and they are derived from first class. m_Base1 and m_Base2 are inherited into two derived classes. Second class has its own m_Base1 and m_Base2 and third class does the same. I am curious. How can second class and third class share the same m_Base1 and m_Base2? You define second class first and enter data into...
17
8375
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
8900
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8758
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,...
0
9276
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9197
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
9129
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
8126
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...
1
6716
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6013
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();...
3
2161
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.