Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a dialog based MFC application. The dialog has a few controls on it one of which is a CtrlList control. I have another class that I am using to do miscellaneous calculations. I am going to pass the results of those calculations into a ListCtrl control.

Within my CDialog class I have a function named CListAdd. Within the CListAdd function I activate a MessageBox to indicate I am in the function. I have a button (button A) on the form that I can click to add data to the ListCtrl by calling the CListAdd function. The data that is added is in the CListAdd function and is not passed in from anywhere else. When I click the button the MessageBox pops up to indicate I am in the CListAdd function. When I dismiss the MessageBox the ListCtrl gets populated with the data as expected.

Within my CDialog class I have another button (button B) that calls a function in my other class. I also have a MessageBox in this function to indicate I am within the function. When I click button B the MessageBox pops up to indicate I am in the function. My next line of code then does a call to the CListAdd function in my CDialog class. The MessageBox pops up indicating I am back in the CListAdd function. However, when I dismisss the MessageBox, the ListCtrl does not get populated with the data.

I can't figure out what is wrong. If I call the CListAdd function from within the CDialog class the control get populated. But if I call the CListAdd function from within the other class the ListCtrl does not get populated. I wanted to get the static data to populate the ListCtrl before I started trying to pass data from the other class into the CListAdd function.

What am I missing?
Posted

1 solution

How are you calling the CDialog class function from the other class?
You must pass the CDialog object pointer to the other class so that it can use that pointer to call the CDialog function.
 
Share this answer
 
Comments
2buck56 7-Nov-10 10:58am    
From within the CDialog button function I call the other function this way:
CEditData *pInfo;
pInfo = NULL;
pInfo->CheckData(0); // call function in EditData.cpp

Then from within the EditData::CheckData function I call the CDialog function this way:
CSmartBlockDlg *pList;
pList = NULL;
pList->CListAdd(0); // call ListAdd in Smart BlockDlg.cpp

I found that if I comment out the pInfo = NULL and the pList = NULL, it works. But I get compiler warnings:
uninitialized local variable 'pList' used and uninitialized local variable 'pInfo' used

And I don't think warnings should be ignored.
«_Superman_» 7-Nov-10 11:07am    
Here is what you're missing.
You have to create an object of a class (instantiate a class) before you can use it to call its methods.
You have only a declared a pointer and no memory has been allocated to it.
2buck56 7-Nov-10 21:29pm    
I'm not sure I understand. I don't want a new object of the class. I just want to call a function in the original class (CSmartBlockDlg) and have it populate the original ListCtrl. However, I changed the code in the CEditData class to this:
CSmartBlockDlg myObj;
myObj.CListAdd(0);
That works and the compiler warning goes away. I wind up in the CListAdd function because my MessageBox pops up and tells me I am there. However, the ListCtrl does not populate with the data that is in the CListAdd function. If I call the CListAdd function from within another function of CSmartBlockDlg, the ListCtrl does populate with the data.
«_Superman_» 7-Nov-10 22:42pm    
I did not mean for you to create new objects of these classes.
In your code you have created pointers which do not point to any object ,which is wrong.
You have to use the existing objects to call the functions.
Since both classes call each others methods, you will need to pass the object address of one class to another so that it can use it to call into the other class.
2buck56 8-Nov-10 13:10pm    
Can you show me a couple of code snippets on how to get the addresss of the Dialog box and pass it to the other function? I did this: CWnd *pWnd = GetDlgItem(IDD_SMARTBLOCK_DIALOG); and passed pWnd to the other function.

I changed the other function to this: void CEditData::CheckData(int DataType, CWnd *myData) but in the CheckData function when i use myData-> the CAddList is not available.

When I call the CEditData function from the CDialog class I will create another object for that. Since I will only be doing some math over there it doesn't matter if it is done with the original class or another instance.

I'm starting to get to the point I'm seriously considering forgetting about the other class and just have my original Dialog class do everything. I was trying to avoid 1500 lines of code in one class but there may not be a way to do that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900