Thread: Sharing a variable between CPropertyPage's

  1. #1
    Registered User KyussRyn's Avatar
    Join Date
    Mar 2009
    Location
    Tokyo, Japan
    Posts
    7

    Sharing a variable between CPropertyPage's

    I am probably going about this the wrong way but I have been searching the web for about 1 week off and on and I can't think for the life of me how to solve this.

    Problem:
    I have an 'Options' PropertyPage which allows a user to change optional values. These values are stored in a [bold]struct[/bold] and are used on the other two PropertyPages. What I want to do is have a central accessable struct that I can reference from the three CPropetyPage's. Now I have tried putting this in the CMainApp .cpp and .h, and in the PropertySheet but I may have made a mistake in the code.

    Request:
    Can you please give me guidance on the best way to have a variable that can be referenced and changed by multple CPropertyPages?

    Code:
    Code:
    Main.h
    typedef struct t_Information {
    	int		serialNumberUndertest;
    	int		testCycles;
    	int		successCycles;
    } Information;
    
    class CMainApp : public CWinApp
    {
    public:
    	CMainApp ();
    	Information testInfo;
    };
    
    extern CMainApp theApp;

    Main.cpp
    Code:
    CMainApp ::CMainApp ()
    {
    
    }
    
    BOOL CMainApp ::InitInstance()
    {
    	AfxEnableControlContainer();
    
    	CTopSheet TopSheet(_T("Test Center"));
    
    	CMainDlg	MainSheet;
    	COptionDlg	OptionSheet;
    	CPollDlg	PollSheet;
    
    	TopSheet.AddPage(&MainSheet);
    	TopSheet.AddPage(&PollSheet);
    	TopSheet.AddPage(&OptionSheet);
    
    	m_pMainWnd = &TopSheet;
    
    	INT_PTR nResponse = TopSheet.DoModal();
    	
    	if (nResponse == IDOK)
    	{
    	}
    	else if (nResponse == IDCANCEL)
    	{
    	}
    
    	return FALSE;
    }
    MainDlg.cpp
    Code:
    CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
    	: CPropertyPage(CMainDlg::IDD)
    {
    	m_hIcon			= AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    	pThisOptions	= &myApp.testingInformation.currentOptions;
    	pThisData		= &myApp.testingInformation;
    	NfcDrv_Import(&myApp.vpNfc);
    	return;
    }
    MainDlg.h
    Code:
    class CMainDlg : public CPropertyPage
    {
    public:
    	ModuleInformation*	pThisData;
    }
    TopSheet.h
    Code:
    class CTopSheet : public CPropertySheet
    {
    protected:
        ModuleInformation*	pThisData;
    }
    TopSheet.cpp
    Code:
    CTopSheet ::CTopSheet ()
    {
    	pThisData	= &testingInformation;
    
    }
    COptionDlg.h
    Code:
    class COptionDlg: public CPropertyPage
    {
    public:
    	ModuleInformation*	pThisData;
    }
    COptionDlg.cpp
    Code:
    COptionDlg::COptionDlg() : CPropertyPage(COptionDlg::IDD)
    {
    	pThisOptions = &myApp.testingInformation.currentOptions;
    }
    Last edited by KyussRyn; 02-24-2010 at 02:45 AM. Reason: Fixed up Code tags

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    With the common variable inside the CPropertySheet class, each page of that sheet can access the variable using GetParent().

  3. #3
    Registered User KyussRyn's Avatar
    Join Date
    Mar 2009
    Location
    Tokyo, Japan
    Posts
    7

    More information on GetParent()

    DaveH, Thank you for your prompt reply. I have put the GetParent() in OnActive in COptionDlg.cpp as a test using the line:
    CWnd* pParent = COptionDlg::GetParent();
    but when I then try:
    pParent->
    the dropdown list of methods and variables does not display the variable (testInfo). I will keep looking into this but if anyone could provide an example of code or more of an explanation it would be much appreciated.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    If CMainDlg is the parent, something like....

    Code:
    CMainDlg* pParent = (CMainDlg *) GetParent();
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User KyussRyn's Avatar
    Join Date
    Mar 2009
    Location
    Tokyo, Japan
    Posts
    7
    Novacain, thanks for the code.

    I am still not getting it though. Let me try and help myself by further explaining. I realised the mistake in my code that CMainDlg was the Parent and that was not what I wanted. It was TopSheet that I wanted to store the common variable. So I now have the below code:
    CMainApp no longer has the line:
    Information testInfo;

    CTopSheet
    Code:
    class CTopSheet: public CPropertySheet
    {
    	DECLARE_DYNAMIC(CTopSheet)
    protected:
    	DECLARE_MESSAGE_MAP()
    public:
    	Information testInfo;
    }
    CMainDlg
    Code:
    class CMainDlg: public CPropertyPage
    {
    	DECLARE_DYNCREATE(CMainDlg)
    public:
    	CMainDlg();
    //	CMainDlg(CTopSheet* pStorage);
    	~CMainDlg();
    	enum { IDD = IDD_OPTIONS_DLG };
    	CTopSheet*		pMyApp;
            Information *		pTestInfo;
    protected:
    	DECLARE_MESSAGE_MAP()
    }
    
    IMPLEMENT_DYNCREATE(CMainDlg, CPropertyPage)
    
    CMainDlg::CMainDlg() : CPropertyPage(CMainDlg::IDD)
    {
    	pTestInfo= &pMyApp.testInfo.currentOptions;
    }
    What I want to do is be able to have one value of Information stored in a central location, that can be retrieved and edited by CMainDlg (and other CPropertyPage's) . If I am going about this the wrong way please let me know.
    I tried the GetParent() function and I kept getting CTopSheet which was fine, but I could not get a consistant value. I am beginning to think that passing pointers might be a better way but if you look at the code // CMainDlg(CTopSheet* pStorage);, I tried this but had eception runtime errors.
    I am open to advice on how to handle this problem. Thank you all for any direction or code to help solve this problem.
    Last edited by KyussRyn; 02-25-2010 at 02:42 AM. Reason: Fixed some errors

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I don't think you have initialised the pMyApp variable before trying to get a pointer to the struct.



    I would just store the struct in the CMainApp class and get a pointer to the struct by using AfxGetMainApp() when needed.

    Depending on the complexity required.....(and as a general pattern of inheritance)


    1 Create a new class derived from CPropertySheet
    2 Add a method to get the Info struct as required (shown below)
    3 Use this derived class as the base class for your property pages.


    Code:
    //[not tested code as I do not have a compiler here]
    
    //get Info struct
    int CPropertyPageBase::GetInfo(Information *pInfo)
    {
              CMyApp *pMyApp=NULL;
              pMyApp = (CMyApp*) AfxGetMainApp();
              if(pMyApp)//we got a pointer to the main app
              {
                         pInfo = &(pMyApp->Information);
                         return SUCCESS;
              }
              return FAIL;
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM

Tags for this Thread