How to access the variables of View Class
-
I created an SDI application. When i click on one of the menu items a dialog box pops-up. I want the values of a few variables that are declared as public in the View class to be accessed and modified in the dialog box. I tried creating an object of the view class in the dialog class, but doesnt work.How do i do it? Please Help.
-
I created an SDI application. When i click on one of the menu items a dialog box pops-up. I want the values of a few variables that are declared as public in the View class to be accessed and modified in the dialog box. I tried creating an object of the view class in the dialog class, but doesnt work.How do i do it? Please Help.
Use
CMyView* myView = (CMyView*)AfxGetMainWnd()->GetActiveView();
to get a pointer to the view. Rahim Rattani Software Engineer, Matrix Systems (Pvt) Ltd., Karachi - Pakistan -
I created an SDI application. When i click on one of the menu items a dialog box pops-up. I want the values of a few variables that are declared as public in the View class to be accessed and modified in the dialog box. I tried creating an object of the view class in the dialog class, but doesnt work.How do i do it? Please Help.
nripun wrote: I tried creating an object of the view class in the dialog class :wtf: Errr, I think this is not a good solution ;P What you need to do is, when creating your dialog, send the variables to the dialog (using a public member function) before calling DoModal. Exemple:
CYourDialog::CDialog
{
// MFC stuff here....public:
void SetVariable(int Value) { m_ViewVar = Value; }private:
int m_ViewVar;
}Then when creating your dialog you can do that:
CYourDialog Dlg;
Dlg.SetVariable(Variable);
Dlg.DoModal();So nowm inside your dialog class know the value of the variable from your View. If you need to retrieve it, the same, add a member function to retrieve the value. Why using functions and not public variable ? In fact this is a good habit to have: your member variables should be private (or protected in case of inheritance) and they can be accessed through Get/Set methods... Hope this helps
-
nripun wrote: I tried creating an object of the view class in the dialog class :wtf: Errr, I think this is not a good solution ;P What you need to do is, when creating your dialog, send the variables to the dialog (using a public member function) before calling DoModal. Exemple:
CYourDialog::CDialog
{
// MFC stuff here....public:
void SetVariable(int Value) { m_ViewVar = Value; }private:
int m_ViewVar;
}Then when creating your dialog you can do that:
CYourDialog Dlg;
Dlg.SetVariable(Variable);
Dlg.DoModal();So nowm inside your dialog class know the value of the variable from your View. If you need to retrieve it, the same, add a member function to retrieve the value. Why using functions and not public variable ? In fact this is a good habit to have: your member variables should be private (or protected in case of inheritance) and they can be accessed through Get/Set methods... Hope this helps