Vivek Kumar
Vivek Kumar

Reputation: 5040

Sharing variable among class instances

class MyApp : public CWinApp {
        afx_msg OnPrefrences();
    };

OnPrefrences() get called when user selects tools->Preference from the menubar.

Now in one dialog(Say DlgX) there is one button, on clicking this I need to open the Preference dialog which has in fact many panes, but here I need to open the Preference dialog by selecting one the these pane as active. Also in that particular pane I need to hide some of the controls only when It gets open through this the dialog not through menu.

So I have created one variable(Say m_varX) in MainFrm class.

void DlgX::OnButtonXClick()
{
   CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
   if(pFrame)
   {
       pFrame->m_varX = TRUE;
      ((CMyApp*)(AfxGetApp()))->OnPrefrences();
      pFrame->m_varX = FALSE;
   }
}

And in button handler of DlgX I have made this m_varX TRUE and call the OnPreference() and after closing of this preference dialog I have made m_varX FALSE.

All this is working fine... But the problem is that things gets clutter in mainFrm. Also the project I am working on is legacy one so I cant make much changes.

Is there any patter available for handling such case?

Thanks

Upvotes: 0

Views: 119

Answers (2)

AndersK
AndersK

Reputation: 36102

You could solve this with a custom dialog (if you don't have it already)

When you show the dialog from the main menu i.e. onPreferences() you fill and show all 'panes'. you would have to do a custom dialog where the ctor takes some arguments.

E.g.

enum { all, part };

void MainFrame::OnPreferences() 
{
  CMyPreferences dlg( GetDocument(), all );
  dlg.DoModal();
}

but when you call it from within a dialog you only fill in the parts you need.

void YourDialog::OnPreferences()
{
  CMyPreferences dlg( GetDocument(), part );
  dlg.doModal();
}

The argument could be something more sophisticated for more fine tuned configuration of what to show/allow to edit.

Upvotes: 1

Felice Pollano
Felice Pollano

Reputation: 33262

I think for that special case, even if sometimes is no more considered a pattern, the singleton pattern would work for you.

Upvotes: 1

Related Questions