Accessing a variable from another class
-
I have 2 class - CUserDlg,CMenuDlg. I collect a string value from CUserDlg and pass/use this value to/in CMenuDlg. I define the variable as Public in CUserDlg and Collect value for it. Public : CString szName; UserDlg.Cpp: szName = "Hai"; CDialog::EndDialog(0); CMenuDlg dlg; dlg.DoModal(); and close CUserDlg screen and calls CMenuDlg screen. I define CUserDlg object in MenuDlg header file. CUserDlg* UserData; And when I access UserData->szName in CMenuDlg.Cpp file, i get only NULL value. Pls help me out.
-
I have 2 class - CUserDlg,CMenuDlg. I collect a string value from CUserDlg and pass/use this value to/in CMenuDlg. I define the variable as Public in CUserDlg and Collect value for it. Public : CString szName; UserDlg.Cpp: szName = "Hai"; CDialog::EndDialog(0); CMenuDlg dlg; dlg.DoModal(); and close CUserDlg screen and calls CMenuDlg screen. I define CUserDlg object in MenuDlg header file. CUserDlg* UserData; And when I access UserData->szName in CMenuDlg.Cpp file, i get only NULL value. Pls help me out.
sugumar wrote: I define CUserDlg object in MenuDlg header file. CUserDlg* UserData; And when I access UserData->szName in CMenuDlg.Cpp file, i get only NULL value. Where do you assign the original CUserDlg pointer to the UserData pointer. I usually pass the pointer via the constructor, but your code doesn't appear to do that. Roughly, UserDlg.cpp
CUserDlg::OnOK() { CMenuDlg dlg(this) dlg.DoModal() }
CMenuDlg.h// forward declare class CUserDlg; class CMenuDlg : public CDialog { public: CMenuDlg(CUserDlg* pUserDlg, CWnd* pParent = NULL); // standard constructor altered private: CUserDlg* m_pUserDlg; }
CMenuDlg.cpp#include "UserDlg.h" CMenuDlg::CMenuDlg(CUserDlg* pUserDlg, CWnd* pParent) { m_pUserDlg = pUserDlg; }
Then when you want to access szNamevoid CMenuDlg::SomeFunc() { // sample use CString temp = m_pUserDlg->szName; }
Michael CP Blog [^] -
sugumar wrote: I define CUserDlg object in MenuDlg header file. CUserDlg* UserData; And when I access UserData->szName in CMenuDlg.Cpp file, i get only NULL value. Where do you assign the original CUserDlg pointer to the UserData pointer. I usually pass the pointer via the constructor, but your code doesn't appear to do that. Roughly, UserDlg.cpp
CUserDlg::OnOK() { CMenuDlg dlg(this) dlg.DoModal() }
CMenuDlg.h// forward declare class CUserDlg; class CMenuDlg : public CDialog { public: CMenuDlg(CUserDlg* pUserDlg, CWnd* pParent = NULL); // standard constructor altered private: CUserDlg* m_pUserDlg; }
CMenuDlg.cpp#include "UserDlg.h" CMenuDlg::CMenuDlg(CUserDlg* pUserDlg, CWnd* pParent) { m_pUserDlg = pUserDlg; }
Then when you want to access szNamevoid CMenuDlg::SomeFunc() { // sample use CString temp = m_pUserDlg->szName; }
Michael CP Blog [^]