Solved

Class for variables...

Posted on 2000-03-02
4
257 Views
Last Modified: 2012-05-04
Hi, I have a dialog based app with a button that opens a property sheet with four pages. The dialog box, property sheet and each of the four pages have their own class. The purpose of the porperty pages is to change and alter variables that are displayed on the dialog box. At the moment I pass the variables to the property sheet class, and then to the respective pages. Naturally this is not pretty because I have about thirty variables. I have heard that it is possible to define a new class, called perhaps variables, and define all my variables in this class. If I need the variables in any other class, I can just access it. Thus there si only one variable in the new class and it is used by the dialog box class, property sheet class and property pages classes. I am struggling in my attempts to implement this. I initialise the class in the dialog based app as the app starts, in the header file in public,

public:
   CVariables var;

and then I use the variables in the dialog box class like this,

var.testvar = 10;

This semi works well. But to access these variables now in the property pages is troubling. If I initialise an instance of the class in the property pages header file such as I did in the dialog header file, it creates a new instance of the variables class, and none of the variables that I gave values to in the dialog box class are carried through to the property pages class. So what should I do? What is the best way to do it? Basically, I need a technique that is smart, cunning and professional. Thanks.
0
Comment
Question by:rose337
4 Comments
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2579220
Here's what you need to do.

In your property page classes add a public pointer to a CVariables object.

public:
   CVariables * pVar;

In the property sheet class add a method which will set the pVar property of and all the pages.

public:
   void setVariables(CVariables * pVar);

In your dialog app class declare a CVariables object (as public as you have done).

public:
   CVariables var;

After you have created the property sheet, set it's pVar property to point to the 'var' property of the app.

   pPropertySheet->setVariables(&var);

Does this answer your question?
Mike.
0
 
LVL 3

Expert Comment

by:Norbert
ID: 2580048
if you use MFC - and I guess you do - you can add your CVariables Instanciation also to your Application class.
like
class CYourAppClass:public CWinApp
{
....
public:
    CVariables m_Vars;
....
}
you can initialize the Vaiables inside your InitApplication Function before the DoModal() call is done.

Doing so you can access the variables anywhere inside your program using

CYourAppClass* YourApp=(CYourAppClass*)AfxGetApp();
YourApp->m_Vars.<DoWhatEverYouWant>
or if you only need a single access to one thing of your Vars
((CYourAppClass*)AfxGetApp())->m_Vars<DoWhatEverYouWant>

This implementation has the advantage that you does not need a defined relation between the caller and the callee like you need when you use mnewton's suggestion.
Think about pPropertySheet->setVariables(&var);
But it has the disadvantage that you have access to these variables from everywhere


0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2580578
In keeping with data encapsulation methodology, and to expand on mnewton's comment, you might want to provide access methods in the CVariables class like so:

public:
     CVariables& GetVariables(){return var;}
     void SetVariables(CVariables* pVar)
{var.x = pVar->x; var.y = pVar->y;} // etc...

private:  
     CVariables var;  // now the data can't be seen outside this class and friend classes, but the access methods will be used to get to it...
0
 

Accepted Solution

by:
sinclairj earned 100 total points
ID: 2590601
You can also use a globally defined structure which contains all your variables.

typedef struct CMyVars
{
  CString var1;
} myVars;

The dialog contains an instance of the structure and initializes the variables in it's constructor or somewhere before DoModal. Each property page contains a pointer to the structure.  

The dialog passes the address of the structure to each property page in its constructor.  Use ClassWizard to create member variables for your controls, then change the DDX so that your variables actually refer to the members of the structure.

Connect to an EditBox:
DDX_Text(pDX,IDC_EDIT1,myVars->var1);

You'll have to add these outside the ClassWizard generated section, and you can add accessors if necessary.  Make sure to do an UpdateData in both the OnApply or OnOK in order to update the structure variables.  Then you can access them directly from the dialog.

I've done this numerous times so let me know if you have any questions.  Hope this answers your question.

Denise
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now