We help IT Professionals succeed at work.

Class for variables...

rose337
rose337 asked
on
301 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.
Comment
Watch Question

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.

Commented:
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


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...
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.