Vb.Net, problem with delegates

Started by
4 comments, last by ZenDarva 19 years, 10 months ago
Ok, i'm trying to use delegates to allow someone using the engine i'm developing to specify handlers for gui elements (Yes yes, i know, i'm the only one gonna use it, but it's still good practice) Anyway, i'm getting the most bizzare error messages, lemme elaborate with what i've tried so far:

Public Delegate Sub ReceiveClick(ByVal x As Integer, ByVal y As Integer, ByVal Buttons() As Byte)


Public Interface infComplex
    Property Top() As Integer
    Property Left() As Integer
    ReadOnly Property Surf() As Surface
    ReadOnly Property Rect() As Rectangle
    Property ZValue() As Integer
    Property Visible() As Boolean
    Sub ReceiveClicks(ByVal X As Integer, ByVal y As Integer, ByVal buttons() As Byte)
    Sub Render()
    Sub move(ByVal x As Integer, ByVal y As Integer)
    Sub Handler(ByVal Handler As ReceiveClick)
End Interface
then, later on, in my clswindows structure...

   Sub handler(ByVal Handler As ReceiveClick) Implements infComplex.Handler
        dHandler = Handler
    End Sub
Ok, and i'm getting this as my error: C:\Documents and Settings\Darva\My Documents\Visual Studio Projects\AsylumEngine\Asylum.vb(530): 'AsylumEngine.clsWindow' must implement 'Sub Handler(Handler As ReceiveClick)' for interface 'AsylumEngine.infComplex'. C:\Documents and Settings\Darva\My Documents\Visual Studio Projects\AsylumEngine\Asylum.vb(639): 'handler' cannot implement 'Handler' because there is no matching sub on interface 'infComplex'. I've also tried it without the public keyword in front of "Delgate sub ReceiveClick", and the error message i get is: C:\Documents and Settings\Darva\My Documents\Visual Studio Projects\AsylumEngine\Asylum.vb(352): 'Handler' cannot expose a Friend type outside of the Public interface 'infComplex'. The only solution i can come up with is stepping out of using interfaces, moving the definition of "Receiveclick" into the base class, and inheriting from the base class. If that's the only way it'll work, i'll do it, but if someone can show me how to make it work the way I'm going now, i'd be happy, because i don't wanna re-write all the menial stuff i'll have to re-write in switching. Thanks Darva [edited by - ZenDarva on June 2, 2004 12:31:49 AM]
Advertisement
I''m pretty new to .Net so I may be wrong but shouldn''t you put the delegate definition inside your interface?
Tried that too. Unfortunatly, it doesn't appear to carry over to the implementing class, and you have to redefine it (which doesn't work either), i forget what exact error message you get, and am too drunk to trust myself opening the ide (Projects die that way, heh) so i'll check tomorrow and post the results.

[edited by - ZenDarva on June 3, 2004 5:28:58 AM]
Ok, i moved the declaration for ReceiveClick into the interface, I''ve got two classes i''m testing it with, clsmap, and clswindow. for the purpose of demonstrating the errors, i''ve defined ReceiveClick in one, and not the other. But both attempt to implement the handler sub from infComplex.

Clsmap doesn''t redefine receiveclick. and the error is:

C:\Documents and Settings\Darva\My Documents\Visual Studio Projects\AsylumEngine\Asylum.vb(355): ''AsylumEngine.clsMap'' must implement ''Sub Handler(Handler As ReceiveClick)'' for interface ''AsylumEngine.infComplex''.

C:\Documents and Settings\Darva\My Documents\Visual Studio Projects\AsylumEngine\Asylum.vb(374): Type ''ReceiveClick'' is not defined.

C:\Documents and Settings\Darva\My Documents\Visual Studio Projects\AsylumEngine\Asylum.vb(376): ''Handler'' cannot implement ''Handler'' because there is no matching sub on interface ''infComplex''.


clsWindow Does redefine receiveclick:

C:\Documents and Settings\Darva\My Documents\Visual Studio Projects\AsylumEngine\Asylum.vb(533): ''AsylumEngine.clsWindow'' must implement ''Sub Handler(Handler As ReceiveClick)'' for interface ''AsylumEngine.infComplex''.

C:\Documents and Settings\Darva\My Documents\Visual Studio Projects\AsylumEngine\Asylum.vb(642): ''handler'' cannot implement ''Handler'' because there is no matching sub on interface ''infComplex''.

Looks to me like i''m gonna have to move to inheritance instead. Please, anyone have any advice?
Not sure if this is your problem, but if you shouldn''t be redefining any type - the ReceiveClick in the class declaration should refer to the same type as the ReceiveClick in the interface.
(Delegates aren''t interchangeable based on a signature - two delegates with the same signature are two different types, and can''t be interchanged.)
I got sick of trying to get it to work, and changed infComplex to a class, renamed it, re-edited everything. It was just easier. Less typing in the long run too i suppose.

This topic is closed to new replies.

Advertisement