By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,924 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,924 IT Pros & Developers. It's quick & easy.

Calling a function from another class (dialog)

P: n/a
Hello Everyone,

This problem's been causing me a lot of trouble and I'm hoping
somebody can help me out!!

I have a dialog-based MFC application in visual studio 2003. I want to
call a function called "OnPrintMsg" from my dialog class called
"MyAppDlg.h". The function needs to be called from another class called
"Client.cpp". The Client class (derived from the CSockets class) exists
as described below.

MyAppDlg.h
....
CClient myclient; //The CClient class is another class that I created.
It inherits from CSocket.
void OnPrintMsg(); // This function is in my dialog application
....
//Client.cpp
CClient::OnReceive() // This function is called from my Client Class
upon receiving information from
// the socket
....

OnPrintMsg(); //want to call function in MyApp dialog with access to
dialog member variables
In a nutshell what I want to do from the OnReceive, is like
CMyApp::OnPrintMsg(); However, when I tried this using a local instance
of the MyApp class, I was unable to access the member variable of the
actual dialog, likely because it was a local instance.

Can anyone help me? Thanks in advance!!

~ Ian

Dec 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
dr**********@gmail.com wrote:
Hello Everyone,

This problem's been causing me a lot of trouble and I'm hoping
somebody can help me out!!

I have a dialog-based MFC application in visual studio 2003. I want to
call a function called "OnPrintMsg" from my dialog class called
"MyAppDlg.h". The function needs to be called from another class
called "Client.cpp". The Client class (derived from the CSockets
class) exists as described below.

MyAppDlg.h
...
CClient myclient; //The CClient class is another class that I created.
It inherits from CSocket.
void OnPrintMsg(); // This function is in my dialog application
...
//Client.cpp
CClient::OnReceive() // This function is called from my Client Class
upon receiving information from
// the socket
...

OnPrintMsg(); //want to call function in MyApp dialog with access to
dialog member variables
In a nutshell what I want to do from the OnReceive, is like
CMyApp::OnPrintMsg(); However, when I tried this using a local
instance of the MyApp class, I was unable to access the member
variable of the actual dialog, likely because it was a local instance.

Can anyone help me? Thanks in advance!!
This falls under FAQ 5.8 (and the rest of section 5), I believe.
Most likely you're in dire need of some education how to deal with
MFC classes. Isn't there a book or, like, a hundred, about those?
Try asking in a newsgroup dedicated to MFC. There are several.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 20 '06 #2

P: n/a
My problem isn't rooted in MFC, the problem is simply classes. The
problem is is I have a main class that has a member variable of another
class. A function within the member variable is called, and I want to
call a function of the main class. There is nothing strictly MFC about
the problem.

Thanks anyway

Victor Bazarov wrote:
dr**********@gmail.com wrote:
Hello Everyone,

This problem's been causing me a lot of trouble and I'm hoping
somebody can help me out!!

I have a dialog-based MFC application in visual studio 2003. I want to
call a function called "OnPrintMsg" from my dialog class called
"MyAppDlg.h". The function needs to be called from another class
called "Client.cpp". The Client class (derived from the CSockets
class) exists as described below.

MyAppDlg.h
...
CClient myclient; //The CClient class is another class that I created.
It inherits from CSocket.
void OnPrintMsg(); // This function is in my dialog application
...
//Client.cpp
CClient::OnReceive() // This function is called from my Client Class
upon receiving information from
// the socket
...

OnPrintMsg(); //want to call function in MyApp dialog with access to
dialog member variables
In a nutshell what I want to do from the OnReceive, is like
CMyApp::OnPrintMsg(); However, when I tried this using a local
instance of the MyApp class, I was unable to access the member
variable of the actual dialog, likely because it was a local instance.

Can anyone help me? Thanks in advance!!

This falls under FAQ 5.8 (and the rest of section 5), I believe.
Most likely you're in dire need of some education how to deal with
MFC classes. Isn't there a book or, like, a hundred, about those?
Try asking in a newsgroup dedicated to MFC. There are several.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 20 '06 #3

P: n/a
dr**********@gmail.com wrote:
My problem isn't rooted in MFC, the problem is simply classes. The
problem is is I have a main class that has a member variable of another
class. A function within the member variable is called, and I want to
call a function of the main class. There is nothing strictly MFC about
the problem.
Quite true. To call a function of your "main" class from outside the
class you can use a pointer to the "main" class object. When you create
or initialize the member object pass it the 'this' pointer from the main
class. Store the pointer in a member variable and use it when need to
call functions in the "main" class object.

--
Scott McPhillips [VC++ MVP]

Dec 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.