Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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...

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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 Month13 days, 7 hours left to enroll

581 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