views:

552

answers:

2

Hi,

I think know how to do this in C# but I'm having syntax trouble in VB.NET because I need to take advantage of the 'handles' event stuff.

I have different events that I somehow (probably delegate) need stored in a property of an attribute (I just add an attribute to a property linking to the corresponding event).

E.g.

<BindEvent(EventThing)>_
Public ReadOnly Property IsTrue() As Boolean
  Get
    Return True
  End Get
End Property

They need to be events so that other programmers can use the VB.NET handles keyword on methods.

Public Delegate Sub TestEvent(ByVal sender As Object, ByVal e As Object)
Public Event E As TestEvent

Public Sub Test() Handles E
End Sub

These properties are already raising a general event, which I am catching, determining the related property and thus getting the related attribute (and delegate/event). I want to then raise the event that's related.

If I just use a delegate tied to the particular event that won't raise other handlers will it?

I also want to avoid having to write a sub for each property that just uses Raise Event on the event type if possible as this seems redundant.

e.g. avoid:

Public Event E As TestEvent
Public Sub CallE(ByVal sender As Object, ByVal e As Object)
  RaiseEvent E(sender, e)
End Sub
<BindDelegate(DelegateToCallE)>_
Public ReadOnly Property IsTrue() As Boolean
  Get
    Return True
  End Get
End Property
A: 

I don't quite understand what you are trying to do. Are you by any chance looking for syntax like this:

Class BindEventAttribute
    Inherits System.Attribute

    Public Delegate Sub TestEvent()
    Public Custom Event EventToRaise As TestEvent
       AddHandler(ByVal value As TestEvent)
          REM TODO: code what happens when anyone calls AddHandler on this event
       End AddHandler

       RemoveHandler(ByVal value As TestEvent)
          REM TODO: code what happens when enyone calls RemoveHandler on this event
       End RemoveHandler

       RaiseEvent()
          REM TODO: code what happens when anyone calls RaiseEvent on this event
       End RaiseEvent
    End Event
End Class
BlueMonkMN
+1  A: 
Public Class foo

    Public Event TestEvent()

End Class


Public Class bar
    Dim WithEvents x As New foo
    Public Sub HandleEvent() Handles x.TestEvent
    End Sub
End Class

Is this what you are looking for?

Maslow
I think it's about as good as it gets unfortunately.
Graphain