Passing WithEvents object btwn Class Module & regular Module

VenturSum

Board Regular
Joined
May 23, 2010
Messages
138
Team,

I'm really stuck on this one. CallByName() might be the right solution. But I'm unfamiliar with that solution. Any insight would be greatly appreciated.

Here goes... I have a live feed from my broker that has to be associated with an object. In this case, it's in a Class module called cMBT and defined as:

Code:
Option Explicit
Option Compare Text

[COLOR=Navy]Public WithEvents[/COLOR] moComMgr [COLOR=Navy]As[/COLOR] MbtComMgr
[COLOR=Navy]Public WithEvents[/COLOR] moOrders [COLOR=Navy]As[/COLOR] MbtOrderClient
[COLOR=Navy]Public WithEvents[/COLOR] moQuotes [COLOR=Navy]As[/COLOR] MbtQuotes

[COLOR=Navy]Public Sub[/COLOR] moOrders_OnLogonSucceed()
.
.
End sub

moComMgr
, moOrders and moQuotes handle events such as logon and routing the inbound datastream.

In a regular Module I define requests such as logging-in. (I could put this in another object such as a userform.) There I have the following code:

Code:
[COLOR=Navy]Public Sub[/COLOR] LoginMBT(sUserName [COLOR=Navy]As[/COLOR] String, sPassWord [COLOR=Navy]As[/COLOR] String)
    
    '-- Establishes Feed Elements -----
[COLOR=Black]    [COLOR=Navy]Set[/COLOR] [/COLOR][COLOR=Black]moComMgr = [COLOR=Navy]New[/COLOR] MbtComMgr
    [COLOR=Navy]Set[/COLOR] [/COLOR][COLOR=Black]moOrders = moComMgr.OrderClient
    [COLOR=Navy]Set[/COLOR] [/COLOR][COLOR=Black]moQuotes = moComMgr.Quotes[/COLOR]
   
    '-- DoLogin returns the state of the connection -----
    '-- This logs into the account ----------------
    mbGotLogin = [COLOR=Black]moComMgr[/COLOR].DoLogin(sUserName, sPassWord)
.
. 
End Code
The problem is that moComMgr, moOrders and moQuotes are not even recognized in the regular module.

Any insight would be greatly appreciated.

The more I think I know, the more I realize that I don't know...

Thanks,
John,
In Annapolis, MD
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
A 'pro' might be able to answer better than me. But as I look at it. In your regular module, I think you need to declare an instance of the cMBT class, then set the various properties of that instance. The moComMgr,moOrders,moQuotes are objects within a cMBT class that happens to have events associated with them. These objects do not make sense independent of a cMBT object. So you first need to instantiate a cMBT object.

Set moComMgr,moOrders,moQuotes etc. of that instance.

So in your regular class try something like,

Dim MycMBT as New cMBT

Set MycMBT.moComMgr = ...
Set MycMBT.MoOrders = ...
etc.

....
Or maybe you should wait for an real expert to answer.
 
Upvote 0
That is exactly the problem. You need to create an instance of the cMBT class first, then assign the objects to it

Code:
Dim objMBT As cMBT
Set objMBT = New cMBT

Set objMBT.moComMgr  = New MbtComMgr

mbGotLogin = objMBT.moComMgr.DoLogin(sUserName, sPassWord)
 
Upvote 0
Teacher...

I never thought I'd need to instantiate a class module??
I'll give it a try. I won't have internet access till tomorrow AM so I'll see what others have to say then. There's got to be a solution, one way or another to handling these events. Others have done it.

Thanks for the suggestion.

John,
In Annapolis.
 
Upvote 0
Okay.
Do I need to re-instantiate the class module in each of the routines it's called. (It's called in about 6 routines in the regulare modules). Or is there a way to instantiate it once??

You both are great.
And I'll be on-line for a while till this is figured out.

John,
In Annapolis, MD
 
Upvote 0
The Class Module is like a blue print for a Custom Object.

You need to 'build' an object from those blue prints first (Instantiate), before you can use any of the Properties or Methods (including objects with events).

It's sort like coding
Name="Bob"
doesn't make any sense.
.Name is a property of an object.

Worksheet.Name wouldn't make sense either. Worksheet is the name of a Class (built into excel), but it is not actually an object, it is just the blue prints for an object. Similarly cMBT.moComMgr doesn't quite make sense either.

Worksheets("Sheet1").Name = "Bob" wold work perfectly, because Worksheets("Sheet1") is a specific instance of the worksheet object that actually exists, and hence you can change the .Name property of that object. Once you've created an instance of the cMBT class, you can go ahead and access the properties and methods of that object.
 
Upvote 0
"Do I need to re-instantiate the class module in each of the routines it's called."

That depends on the scope of the Instance when you declare it. I'm not sure how you are using cMBT objects in your project, but generally speaking, it is better to declare variables locally. So, unless there is a reason not to, I'd just declare it in each routine.
 
Upvote 0
Guys,

I'm getting the following error:
"User defined type not defined"

Thoughts?

John,
In Annapolis, MD
 
Upvote 0

Forum statistics

Threads
1,224,540
Messages
6,179,417
Members
452,912
Latest member
alicemil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top