Link to home
Start Free TrialLog in
Avatar of BobsExEx
BobsExEx

asked on

Communicating between Module and Class Module


Hi

I have a DLL project that has a module and a class module.
For some reason, I cannot call the class module functions
from the module.

1.  Why is this?
2.  Any suggested work-arounds?

Reasons for doing this:
I would like the class to raise an event - something I cannot do from
the regular module (right?).  Some of the code in the module CANNOT
exist in a class module because it uses the AddressOf function.  So
I have to go back and forth between the two with the functionality.

Bob
Avatar of aeklund
aeklund

1. You cannot call a function within a class unless you create the object:
 dim o as new Class1
 o.Function

2. I just did the same thing, here is how I did it..

In your module declare a module level variable of your class: Public m_oClass as Class1

In your class Initialize Event set that public module variable to the same instance of the class:
Private Sub Class1_Initialize()
  Set m_oClass = Me
End Sub

In your class Terminate Event set that public module variable to nothing to clean up the resources
Private Sub Class1_Terminate()
  Set m_oClass = Nothing
End Sub

Now, the call... In your module you can now call any public property, sub, function in your class: Call m_oClass1.Function

To hide the sub/function from out of process application, declare it with the Friend keyword: Friend Sub MyFunction()

Hope that helps...
Here is a working example of the above:

'module code:
Option Explicit

Public m_oObject As Class1

Public Sub CallBackFunction()
  Call m_oObject.DoSomething
End Sub

'class code: (name=Class1)
Option Explicit

Private Sub Class_Initialize()
  Set m_oObject = Me
End Sub

Private Sub Class_Terminate()
  Set m_oObject = Nothing
End Sub

Friend Sub DoSomething()
  Msgbox "It worked!"
End Sub
Avatar of BobsExEx

ASKER

Interesting idea.  I have already put in what you said and it is similar to your later message.

However, everytime I try to run it (by starting the app, then switching to another app so that a WM Message comes through), the entire IDE crashes without a message or anything.

When I comment out the call to the class sub, then the IDE doesn't crash (even keeping all declarations and "set"s the same).  I have error handling in there but it doesn't even get to that point.

Interesting idea.  I have already put in what you said and it is similar to your later message.

However, everytime I try to run it (by starting the app, then switching to another app so that a WM Message comes through), the entire IDE crashes without a message or anything.

When I comment out the call to the class sub, then the IDE doesn't crash (even keeping all declarations and "set"s the same).  I have error handling in there but it doesn't even get to that point.

ASKER CERTIFIED SOLUTION
Avatar of aeklund
aeklund

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
aeklund

Yeah, I had replaced the msgbox with a debug.print just in case.

I am going to play around with it some more and I will get back to you.

You get the prize, bro.

I appreciate your work on this so I threw an extra 250 pts on.  I don't think I would have figured that out.

It still runs with some instability but I think it has to do with my running in the IDE and breaking out before messages are processed correctly.

Thanks
oh - I can't add award more than 500 per question...

Thanks again.
Very nice.  Tough stuff.

Bob
Glad to help.  Thanks for the points...  I was stuck on a similar problem and took me some time to figure it out, but this is how I did it and it works like a charm.