|
-
Jun 8th, 2002, 09:58 AM
#1
Thread Starter
Fanatic Member
Using events in Interface classes
Hi,
Is it possible to declare events in your interfaces? I want to use an interface class to provide a standard interface to my app. Here is an example of what I'm trying to do:
VB Code:
PublicNotCreateable Class MyInterface()
Public Event InterfaceEvent(parameter as Variant)
Public Sub InterfaceMethod()
'
End Sub
End Class
Public Class InterfaceImplemented()
Implements MyInterface
Private Sub MyInterface_InterfaceMethod()
MsgBox "This is an interface method"
End Sub
End Class
Public WithEvents MyObj as MyInterface
Private sub Form_Load()
Set MyObj = New InterfaceImplemented 'The error occurs here because
'vb says I haven't implemented the event.
'This is all very good and well, but how do I do that?
End Sub
The class declaration is just to show you the different classes. Thanks
-
Jun 14th, 2002, 02:22 PM
#2
Hyperactive Member
Looks like you have done here:
Code:
Public Event InterfaceEvent(parameter as Variant)
However, this obviously isn't working for you. Looks like you're using .NET since you're declaring classes this way, so I can't really comment. I know that VB6 will do events this way, as long as you declare your object using the WithEvents keyword, which you have done, so...
In theory, you would implement the event like this:
Code:
PublicNotCreateable Class MyInterface()
Public Event InterfaceEvent(parameter as Variant)
Public Sub InterfaceMethod()
'
End Sub
End Class
Public Class InterfaceImplemented()
Implements MyInterface
Private Sub MyInterface_InterfaceMethod()
MsgBox "This is an interface method"
End Sub
Private Sub MyInterface_InterfaceEvent(RHS As Variant)
MsgBox "This is an interface event"
End Sub
End Class
Public WithEvents MyObj as MyInterface
Private sub Form_Load()
Set MyObj = New InterfaceImplemented 'The error occurs here because
'vb says I haven't implemented the event.
'This is all very good and well, but how do I do that?
End Sub
Private Sub MyObj_InterfaceEvent(RHS As Variant)
' object event
End Sub
-
Jun 16th, 2002, 07:47 AM
#3
Thread Starter
Fanatic Member
Hi ChiefRB,
I am in fact using VB6 and not .NET. This is the problem. I know it can be done very easily in .NET, but we do not have licenses to develop in .NET, so I am stuck with VB6. The reason for the class declaration there, was just to show the separate classes that I was using.
I tried your method below, but with no luck. It still gives me the error :
459 - Object or class does not support the set of events
on this line
VB Code:
Set MyObj = New InterfaceImplemented
I understand why though. Its looking for an event declaration in the InterfaceImplemented class which has the form
VB Code:
Private event Interface_InterfaceEvent(RHS As Variant)
But vb highlights this as incorrect syntax. There must be another way of doing this. If there isn't, then I don't see why Microsoft bothered putting polymorphism into VB6 at all since it is so limited.
I have found plenty of examples in msdn implementing Methods and properties, but none implementing events.
I think I might make a jump to C++ for this one. I can provide an interface with events in C++ can't I? (learning cure)
-
Jun 16th, 2002, 04:01 PM
#4
Hyperactive Member
Alright, in the example I gave above, when in the form that is implementing the interface, can you select the interface from the top left combo box? If so, you should be able to explore stuff further. Push F2 to use the object browser as well.
If this isn't gonna work this way, then you have to use Early Bound events. This basically means that you have two interfaces, one for events (which are actually just normal methods - subs or functions) and one normal one with an added SetEventHandler method. This method will accept an object of the events interface.
You can have whatever you want in either (subs, functions, properties - just like normal interfaces).
Now, when you want to catch the events, you simply implement the IEvents interface. Use the SetEventHandler method to give the class a local reference to yourself, which is can then use to "raise" events (methods of IEvents). Confusing I know... 
Code:
'----------------------------------------------------------------------------
' IEvents:
Public Sub Event1()
End Sub
Public Function Event2(ByVal Param As Integer) As Integer
End Sub
'----------------------------------------------------------------------------
' IYourObject
Public Property Get YourProp() As String
End Property
Public Sub YourSub()
End Sub
Public Sub SetEventHandler(EH As IEvents)
End Sub
'----------------------------------------------------------------------------
' CYourObject - implements the IYourObject interface
Option Explicit
Implements IYourObject
Dim m_Events As IEvents
' interface implementation
Public Property Get YourProp() As String
' raise an event
m_Events.Event1
End Property
Public Sub YourSub()
' raise an event
m_Events.Event2
End Sub
Public Sub SetEventHandler(EH As IEvents)
Set m_Events = EH
End Sub
'----------------------------------------------------------------------------
' in the form that is going to create the CYourObject
Option Explicit
Implements IEvents
Dim MyClassObj As IYourObject
' IEvents implementation
Public Sub IEvents_Event1()
MsgBox "Raised Event1 !!"
End Sub
Public Function IEvents_Event2(ByVal Param As Integer) As Integer
MsgBox "Raised Event2 !!"
End Sub
' form code
Private Sub Form_Load()
' create the object
Set MyClassObj = New CYourObject
MyClassObj.SetEventHandler Me
End Sub
Hope this is of use.
-
Jun 17th, 2002, 02:08 AM
#5
Thread Starter
Fanatic Member
LOL
Where did you learn this one. Looks like quite a good idea. I will give this a try.
Maybe I am going about my problem the wrong way. What I am trying to do is give a standard interface for other developers to develop certain components for my app, really like plug inns.
The application I am developing is a type of data transport and parsing service. In this application I have defined 3 main modules to be a part of my DataPipe object. Receivers, Parsers and Senders. I was considering having an IReceiver interface, an IParser interface and an ISender interface. We have many sources of data, such as data received via RS232, message queueing systems database queues etc. and of course the data can be sent off in similar ways. The data also comes in different forms from each. The main criteria here is that the Receiver receives data and raises an event informing the application of this. The application reads the data in as a string and then calls a function of the IParser interface, passing the data on as a string. The parser should then parse the data string and return an xml string which the application then passes on to the sender which then handles sending of the data.
I was hoping that the interface would enable other developers in the company to add their unique data receivers, senders and parsers to the application and also aid in our versioning problems.
What problems could I encounter in doing this? I ask this because I have actually never done this type of thing before.
How would I specify that the application should instanciate one of their objects. I am guessing I would need an initialization file and the proceed with something like:
VB Code:
Dim plugIn as string
Dim newObj as IReceiver
Set newObj = CreateObject(plugIn)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|