Dynamic create withevents object for different class module

Posted on 2003-03-27
Medium Priority
Last Modified: 2013-11-23
now i have a class prja.clsa in vb6.0
in a normal way, i can write this to create withevents object

dim WithEvents a As clsa
set a = new clsa

how about now i would like to create without reference the class at the beginning.
can i write sth like this to make it works?

dim withevents a as object
set a = new createobjectwithevents("prja.clsa")

Question by:cnfei
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Accepted Solution

gepi earned 300 total points
ID: 8222985
No, you can't do that, because VB needs the Interface of the object to implements the Events.
But, you can do other stuff like a general event object.
'Implementation for clsEvents class.
Event EventRaised(vntValue as Variant)

Public sub Raise(vntValue as Variant)
  RaiseEvent EventRaised(vntValue)
End Sub

'You have to Declare an Interface like IEvent with this property inside

Public Property Set EventObject(objEvent as clsEvent)
End Property

'Use it in other classes let say clsMyClass
Implements IEvent
Private mobjEvent as clsEvents

Property Set IEvent_EventObject(objEvent as clsEvent)
  Set mobjEvent = objEvent
End Property

Public sub DoStuff()
  'Do something here
  'Now, raise the event
  mobjEvent.Raise "MyEvent"
End Sub

Private sub Class_Terminate()
  Set mobjEvent = nothing
End Sub

'This line will be in form code module.

Private WithEvents mobjEvent as clsEvent
Private mobjObject as Object

Private sub SetEvent(objEventSupport as Object)

  On Error GoTo ERRH
    Set objIEvent=objEventSupport
    Set objIEvent.EventObject=mobjEvent
  Err.Raise vbError+10,"Set Event", _
"No event interface supported"

End Sub
Private Sub Form_Load()

  Dim objIEvent as IEvent
  Set mobjEvent = New clsEvent
  set mobjObject = CreateObject("[Project].clsMyClass")
  SetEvent mobjObject

End Sub

Private Sub mobjEvent_EventRaised(vntValue as Variant)
  'Treat you event
End Sub

Private Sub Form_Unload(Cancel as Integer)

  Set mobjObject = Nothing
  Set mobjEvent = Nothing

End Sub

I hope this will help you.

Author Comment

ID: 8236255
As i am not that familiar with the usage of Interface and Implements, what do you mean by declare an interface like IEvent?

I am now using vb6.0 not vb.net, there is interface function for vb6.0 too? if yes, could you give me examples on it too?

Expert Comment

ID: 8236498
It is very simple to create an interface.
In VB 6.0 an interface it's a class which contains only the declarations of the methods.
So, the IEvent class, will be a normal class which has inside only the property EventObject without other code...
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8236501
Give to me your wmail address, and I will send to you an example.

Author Comment

ID: 8236899
my email address is cnfei@hkem.com
thanks a lot.

Author Comment

ID: 8242568
The problem has been solved. Thanks a lot.

Expert Comment

ID: 8243272
Any time...

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question