473,394 Members | 1,746 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,394 software developers and data experts.

Passing Data From One Class to Another

I need a member variable in class A known within an instance of class
B, even though B does not instantiate A.

My solution was to make the member variable private static, and create
a public static accessor function that returns the variable's value. I
reference this function in class B as ClassA::GetValueOfVariable().

Although this works, I wonder if there is a better way.
Jul 22 '05 #1
1 2791
"Nobody You Know" <ap*******@hushmail.com> wrote...
I need a member variable in class A
"Variable in class" certainly hints at a static data member. Is
that what you're saying here? Or do you mean "any variable
declared in the class A definition", i.e. it could be non-static?
known within an instance of class
B, even though B does not instantiate A.
So,

class A {
SomeOtherType membervariable; // non-static for now
...
};

class B {
RetValType somememberfunction() { // non-static member func
???
You have to define the term "known"
and where the A comes from (see below)
???
}
};
My solution was to make the member variable private static, and create
a public static accessor function that returns the variable's value. I
reference this function in class B as ClassA::GetValueOfVariable().

Although this works, I wonder if there is a better way.


Well, whether that member is static or not is unspecified in your
statement. If it's to be static, your solution is fine. If it is
to be non-static (but per instance of A), your solution is simply
wrong, don't you think?

An instance of B can "know" an instance of A, if the A is passed to
B's member function as an argument, even if B does not instantiate
the A itself. That may change the solution, no?

Victor
Jul 22 '05 #2

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
by: Simon Harvey | last post by:
Hi, In my application I get lots of different sorts of information from databases. As such, a lot of information is stored in DataSets and DataTable objects. Up until now, I have been passing...
6
by: glenn | last post by:
I have a customer class that contains all of my fields and properties related to a single customer. I then have a dataaccess class that knows how to read and write a customer class to and from the...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
10
by: Stan | last post by:
There are two ways to pass structured data to a web service: xml === <Order OrderId="123" OrderAmount="234" /> or class =====
3
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
0
by: Iain McIntosh | last post by:
Hello if anyone can help me with this I will be very grateful. I have a working version of this program as a windows application, when I try to port it over to c# ASP.NET I can't make it work. ...
5
by: Markus Ernst | last post by:
Hello A class that composes the output of shop-related data gets some info from the main shop class. Now I wonder whether it is faster to store the info in the output class or get it from the...
9
by: itarizin | last post by:
Today I found as ignorance owe me.. Let me explain what I'm tring to do and fail (fail: in my needs) For example, I've my stupid class: public class Hello { private int x = 0; public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...
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.