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::GetValu eOfVariable().
Although this works, I wonder if there is a better way. 1 2806
"Nobody You Know" <ap*******@hush mail.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 somememberfunct ion() { // 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::GetValu eOfVariable().
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 code...
TCHAR myArray;
DoStuff(myArray);
|
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 around chunks of data in
DataTables/DataSets, simply because that was the format that they were in
when the data was taken from the database. Now, I know this maybe a pretty
silly question with a standard "it depends" answer, but I'm going to...
|
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 database.
However, I want to have my customer class be the one that actually calls
the data access class and have it pass itself as a reference to the data
access class. This would allow the data access class to just write the info
in the...
|
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 click a button to
determine whether the zip code is unique. If the zip code is not unique,
another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip
code is passed to the lookup form/dialog by reference. I then load a...
|
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 the custom employee class and have built
it as a separate library (employee.dll). This employee.dll is being
referenced by both the web service and the windows application.
I face the following problem when I send this class to the webservice.
| |
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
=====
|
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 runtime error,
" Attempting to call into managed code without transitioning out first.
Do not attempt to run managed code inside low-level native
extensibility points, such as the vectored exception handler, since
doing so can cause corruption...
|
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.
The program works in the following way.
Uploads a file to the server. (works)
Populates a dataset (works)
Passing the dataset to another class (doesn't work)
|
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 main class whenever it is
needed:
class shop_main {
var $prices = null;
function &get_prices() {
|
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 Hello()
|
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...
| |
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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,...
|
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...
|
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...
|
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...
| |
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();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |