Solved

Class for variables...

Posted on 2000-03-02
4
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 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