accessing the base class
-
hi , i have a dialog based application having a class for example CDialog. in the on button click event iam loading another dialog as follows: m_pModeless= new dlg; Then after getting the safeHwnd : m_pModeless->Create(......); where dlg is the class of the new dialog and m_pModeless has been declared in the header file as dlg *m_pModeless. Now i want to access the member variables from this child window to the main window i.e from one class to the other. I'am a bit confused of how to go about it ... please help. Thanking you. Satadru.
-
hi , i have a dialog based application having a class for example CDialog. in the on button click event iam loading another dialog as follows: m_pModeless= new dlg; Then after getting the safeHwnd : m_pModeless->Create(......); where dlg is the class of the new dialog and m_pModeless has been declared in the header file as dlg *m_pModeless. Now i want to access the member variables from this child window to the main window i.e from one class to the other. I'am a bit confused of how to go about it ... please help. Thanking you. Satadru.
I assume you are attempting one of the following procedures: 1. Accessing the newly created dialog from the button click event 2. Accessing the class running the button event from the newly created dialog For case #1, the answer is simple: you already have a pointer to the class, and it is properly initialized by using
new
. You can use this pointer to alter the data members as you see fit. For case #2, you need to specify the calling class as the parent of the newly created dialog. For modeless dialog creation, the calling convention would beCDialog::Create( UINT uiTemplateID, CWnd* pParent )
. Specifying the this keyword into the parent pointer would designate the calling class as the parent of the newly created modeless dialog. Further in case #2, you can access the parent window by usingGetParent
method, which will return a pointer to the parent class'sCWnd
. Because you have derived fromCDialog
, and becauseCDialog
is derived fromCWnd
, you can use the pointer returned byGetParent
and cast it down to the appropriate class. Understanding this, here is a code fragment, which will do the casting operation. Remember that if you put this casting operation into a function inside the newly created dialog, you must include the header file of the parent dialog into the implementation file. Otherwise the casting operation is unable to determine the type of the variable. In here, we assume that the parent class (which handles the button click), is calledCParentDialog
and the child dialog (newly created one) is calledCChildDialog
.// In the beginning of the implementation, include the header of parent
#include "ParentDialog.h"...
// Further down the road, a member function of CChildDialog, get the parent class and cast it down
CParentDialog* pParentDialog = DYNAMIC_DOWNCAST( CParentDialog, GetParent() );// Use the pointer to access data member/methods
pParentDialog->SomeMethod( someParams );
pParentDialog->SomeData = SomeValue;This functionality comes directly from the way MFC is designed. Have a look at the inheritance map (hiearchy chart). In C++, it is always possible to cast a pointer from the base class to the derived class, and in MFC,
-
hi , i have a dialog based application having a class for example CDialog. in the on button click event iam loading another dialog as follows: m_pModeless= new dlg; Then after getting the safeHwnd : m_pModeless->Create(......); where dlg is the class of the new dialog and m_pModeless has been declared in the header file as dlg *m_pModeless. Now i want to access the member variables from this child window to the main window i.e from one class to the other. I'am a bit confused of how to go about it ... please help. Thanking you. Satadru.
Hi You should pass a pointer on the main window on constuctor of child dlg. //Forward declaration class CMainDlg; class CChildDlg : public CDialog{ CChildDlg(CMainDlg* p_main_dlg):CDialog(...){ this->p_main_dlg = p_main_dlg; //Now throught this->p_main_dlg you can get //allother public members of main dlg } private: CMainDlg* p_main_dlg; }; class CMainDlg : public CDialog{ ... paublic: int var; void CraeteChild(){ CChildDlg dlg(this); dlg.DoModal(); } };
-
I assume you are attempting one of the following procedures: 1. Accessing the newly created dialog from the button click event 2. Accessing the class running the button event from the newly created dialog For case #1, the answer is simple: you already have a pointer to the class, and it is properly initialized by using
new
. You can use this pointer to alter the data members as you see fit. For case #2, you need to specify the calling class as the parent of the newly created dialog. For modeless dialog creation, the calling convention would beCDialog::Create( UINT uiTemplateID, CWnd* pParent )
. Specifying the this keyword into the parent pointer would designate the calling class as the parent of the newly created modeless dialog. Further in case #2, you can access the parent window by usingGetParent
method, which will return a pointer to the parent class'sCWnd
. Because you have derived fromCDialog
, and becauseCDialog
is derived fromCWnd
, you can use the pointer returned byGetParent
and cast it down to the appropriate class. Understanding this, here is a code fragment, which will do the casting operation. Remember that if you put this casting operation into a function inside the newly created dialog, you must include the header file of the parent dialog into the implementation file. Otherwise the casting operation is unable to determine the type of the variable. In here, we assume that the parent class (which handles the button click), is calledCParentDialog
and the child dialog (newly created one) is calledCChildDialog
.// In the beginning of the implementation, include the header of parent
#include "ParentDialog.h"...
// Further down the road, a member function of CChildDialog, get the parent class and cast it down
CParentDialog* pParentDialog = DYNAMIC_DOWNCAST( CParentDialog, GetParent() );// Use the pointer to access data member/methods
pParentDialog->SomeMethod( someParams );
pParentDialog->SomeData = SomeValue;This functionality comes directly from the way MFC is designed. Have a look at the inheritance map (hiearchy chart). In C++, it is always possible to cast a pointer from the base class to the derived class, and in MFC,
hi thanks for your help ; but theres a problem:- inspite of including the header file of the main dialog viz CMainDlg its still giving me an error saying: 'classCMainDlg': is not a member of 'CMainDlg' Could you please tell me why is this happening.:(( thanking you, Satadru
-
hi thanks for your help ; but theres a problem:- inspite of including the header file of the main dialog viz CMainDlg its still giving me an error saying: 'classCMainDlg': is not a member of 'CMainDlg' Could you please tell me why is this happening.:(( thanking you, Satadru
What is this 'viz' ? "Which is" ? The error you are describing is related to the CMainDlg itself. You are referring to a member
classCMainDlg
somewhere in your code, and this member is not declared in the header file of the class. See the header file of CMainDlg for details, especially check the line where the error is. The case #2 I posted back in my previous message did not tell you to add any member to the parent (CMainDlg) class. If you added members there, then you have done something wrong yourself. In conclusion: this error has nothing to do with the method I described. It is happening because a) you have done something wrong yourself or b) have not understood my description properly. I would ask you to post the line where this error is happening and the context where it is in. I believe that it might be in the line where you create the modeless dialog box. Remember, that the 'this' pointer always points to the current object that is executing a method. For example, if you had an object of a class created, returning 'this' from a function would return the address of the object. Check your code and post the line where the error is happening. You probably have just mistyped something. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible. -
What is this 'viz' ? "Which is" ? The error you are describing is related to the CMainDlg itself. You are referring to a member
classCMainDlg
somewhere in your code, and this member is not declared in the header file of the class. See the header file of CMainDlg for details, especially check the line where the error is. The case #2 I posted back in my previous message did not tell you to add any member to the parent (CMainDlg) class. If you added members there, then you have done something wrong yourself. In conclusion: this error has nothing to do with the method I described. It is happening because a) you have done something wrong yourself or b) have not understood my description properly. I would ask you to post the line where this error is happening and the context where it is in. I believe that it might be in the line where you create the modeless dialog box. Remember, that the 'this' pointer always points to the current object that is executing a method. For example, if you had an object of a class created, returning 'this' from a function would return the address of the object. Check your code and post the line where the error is happening. You probably have just mistyped something. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.hi , i have tried it out ..but the same problem exixts... i cudn't figure out where its going wrong ..can i have your email id so that i'll attach a small code snippet which is posing me the problem. I gratefuly appreciate your kind gesture. Thanking you. Satadru.
-
hi , i have tried it out ..but the same problem exixts... i cudn't figure out where its going wrong ..can i have your email id so that i'll attach a small code snippet which is posing me the problem. I gratefuly appreciate your kind gesture. Thanking you. Satadru.
Just paste the code here, and I will analyse it promptly. I prefer not to give my e-mail address away on public forums, they tend to end up to mischiveous hands :suss: Besides, if your code had something so important that you couldn't post it here, how could you send it on e-mail either ? :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.