Passing an Event as a Parameter?

Arokh

Centurion
Joined
Apr 11, 2006
Messages
124
I'm wondering if that is somehow possible.

I've written a program which uses plugins,
now I want the PluginAPI to offer some custom Events.
Custom as in not some controls Events, rather a declared event which is then raised somewhere in my code.

Now I know only 2 solutions which I'd rather not use:
Move all the declarations of the Events to the class which is passed to the Addoninterface, which is usable by the addonwriter,
or pass the whole class where the Event declarations reside, also making any other public declared object accessible.

Here is an example:
Visual Basic:
Class MainProgram
    Public Event Bla()
End Class

Public Class cAddons
    Interface iHost
    End Interface

    Interface iAddon
        Sub Initialize(ByVal Host As iHost)
    End Interface
End Class
iAddon is the Interface which the addonwriter has to implement.
The API is provided though the iHost Interface,
which is where I want to make the Events, located in MainProgram, available.
 
Well I can add them there but I can't seem to assign an event to another.

Since the Event in the MainProgram class will be called,
I don't know how to make it accessible without exposing the whole MainProgramm class.

Visual Basic:
Class MainProgram
    Public Event Bla()
End Class

Public Class cAddons
    Interface iHost
        Event BlaCopy()
    End Interface

    Interface iAddon
        Sub Initialize(ByVal Host As iHost)
    End Interface
End Class
Now the addonwriter can access BlaCopy within the Initialize method through:
AddHandler Host.BlaCopy, [SomeFunction]

But the Event Bla & BlaCopy are still different Events.
Something like BlaCopy = Bla doesn't work,
I don't know how to connect them.
 
Perhaps I'm being a bit thick but I'm still not sure exactly what you are after.

Are you wanting the main program to raise the events on behalf of the add ons or are you wanting the add ons themselves to raise the event?
 
Yes the Events are raised (and declared) in the main program.

I want to give the addonwriter the ability to react to certain Useractions.
For example when the main program opens up the settingsform,
the Event SettingsOpened is raised.

Now I want to make the Event "SettingsOpened" available to the addonwriter,
so he knows when to add the addonsettings-controls to the settingsform.
 
You could either have the add ons raise an event that the main program handles by simply re-raising the event or provide a callback routine to the add on as an alternate means of notification.
 
I don't know if I understood everything correctly,
but it helped, it works now.

Instead of passing the Event to the addon (dll),
the addon calls a function (from the main program) which takes the address of the function which should be called and
then the mainprogram adds a handler for it.

I guess this method is the later possibillity you meant.

I don't understand the former one though.
Sure I could add an Event into the Addon Interface and add a handler for it in the main program,
but since the addon doesn't know when the settingsform is being opened by the mainprogram,
I don't know how this could be useful.
 
MainProgram needs to implement iHost

Visual Basic:
Class MainProgram
    Public Event Bla()
End Class

Public Class cAddons
    Interface iHost
        Event BlaCopy()
    End Interface

    Interface iAddon
        Sub Initialize(ByVal Host As iHost)
    End Interface
End Class

This won't work for the simple reason that MainProgram does not implement the iHost interface. If MainProgram implemented the interface, declared an event BlaCopy (which it would have to), and then you passed an instance of MainProgram to the addon class, then it would work correctly.

If you don't wish to pass MainProgram to your addon, create a proxy class which also implements iHost and which handles the MainProgram's BlaCopy event before raising its own BlaCopy. It looks like that's what you were trying to do with cAddons, but I'm not sure why the interfaces are nested types.

Good luck :cool:
 
Back
Top