Solved

Class for variables...

Posted on 2000-03-02
4
258 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

914 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

12 Experts available now in Live!

Get 1:1 Help Now