-1

I am developing MFC based SDI application in VC++ derived from CFormView class.In my dialog I have a checkbox. When this check box is clicked or not I want to get the state of this check box in main class say CDemoView.cpp and use that particular state for some calculations in another class say OServer.cpp which is a C++ class. I tried using SetCheck(), GetCheck() functions and I am failing.How can I get the state of checkbox in my other class??

Thanks in advance

Ferin Jose
  • 45
  • 1
  • 2
  • 9

2 Answers2

2

Take a bool variable in the class.set its state according the checkbox.access that variable in other class to get the state of your check box.

MoBaShiR
  • 442
  • 3
  • 12
  • I tried it..It is failing I declared as follows in header file BOOL m_IsChecked = TRUE; which sets the state of check box to true.. But I cant use this variable in my second class.. It causes error showing m_IsChecked undeclared identifier.What is the error here? – Ferin Jose Oct 18 '13 at 12:33
  • You cannot access any member directly from outside the class.To access m_IsChecked say it is declared in your view class. And let pView is the pointer to your view class.Then in your outside class say MainFrame etc.. use pView->m_IsChecked to access the variable of view in mainframe class. – MoBaShiR Oct 20 '13 at 09:52
  • I tried that too..But now I am getting an unhandled exception. In second clas I declared as follows in a function: CECUSimulatorView *pView = (CECUSimulatorView*)GetParent(NULL); if(pView->m_nEnableLogging) { ----- } I am getting an unhandled exception in the if statement. Now why is it so?? Please Help.I could not understand – Ferin Jose Oct 21 '13 at 05:23
  • CECUSimulatorView pView = (CECUSimulatorView)GetParent(NULL); is not the correct way. i hope this link may help you:http://forums.codeguru.com/showthread.php?281430-MFC-Doc-View-How-to-obtain-a-pointer-to-various-objects – MoBaShiR Oct 21 '13 at 13:42
0

The "other class" needs a variable that will let it access the view object that contains the check box state. Something like

if (pview->m_IsChecked)

or even

if(*pbool_IsChecked)

Initialize this pointer by passing the appropriate address to "other class" in its constructor or in a function you add for this purpose.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
  • I tried that too..But now I am getting an unhandled exception. In second clas I declared as follows in a function: CECUSimulatorView pView = (CECUSimulatorView)GetParent(NULL); if(pView->m_nEnableLogging) { ----- } I am getting an unhandled exception in the if statement. Now why is it so?? Please Help.I could not understand – Ferin Jose Oct 21 '13 at 05:26
  • GetParent returns a pointer, not an object. So it is an error to cast the pointer into an object. CECUSimulatorView* pView = (CECUSimulatorView*)GetParent(NULL); – ScottMcP-MVP Oct 21 '13 at 12:59
  • Could you please help me with code portions to get pointer to view class? I am getting error always – Ferin Jose Oct 22 '13 at 09:17
  • You are doing this in "another class" but you have not said if that class is a child window of the view. If it is not then using GetParent makes no sense because the view is not its parent. I already suggested you pass a pointer into "another class" that it can use to access the view or the bool. Did you work on that? – ScottMcP-MVP Oct 22 '13 at 15:14
  • I got pointer to my view class. I used the following code CECUSimulatorView *pView = (CECUSimulatorView*)(RUNTIME_CLASS(CECUSimulatorView)->CreateObject()); But now my problem is I have a bool variable in view class to determine the state of check box. The state cannot be obtained in my second class. Second cls is not the child class of view class entirely a pure c++ class..I tried using pView->m_Isset but it always has the default bool value False..Why is it so? Please Help @ScottMcP-MVP – Ferin Jose Oct 23 '13 at 06:36
  • "CreateObject" creates a new view object. That's not the same as the view object that you are trying to access. So this approach is misguided. It also seems that you don't understand pointers: You are casting a pointer to a view, not to a view pointer. One approach I have already suggested is that, from the view, you call a function in second class, passing it the view's 'this' pointer. The second class should save the pointer in a member variable and use it to access a view member. There should be no casting: Accept a view pointer parameter and store it in a view pointer variable. – ScottMcP-MVP Oct 23 '13 at 13:51