Hi Chuck
The delegate and event concepts are important and have to be implemented in Cobra. I will try to describe what I mean with the delegate an event mess in VB. This description is a black box perspective on delegates and events. I have never looked at any dissembled code and will probably never do it. Neither am I an expert on these issues, just confused, so I have probably misunderstood many of the issues here In VB you can’t assign directly a method to a variable like this:
- Code: Select all
aSub =anotherSub
You have to first declare a delegate:
- Code: Select all
Public Delegate Sub AnyParameterLessSub()
Then:
- Code: Select all
Dim aSubDelegate As AnyParameterLessSub = New AnyParameterLessSub (AddressOf anotherSub)
An ignorat question, why is AddressOf needed here? It seems that Cobra is using the Ref keyword in similar situations. Is it a problem here to understand that it should be the reference? The delegate class methods like Remove, RemoveAll and Combine do not require the AddressOf a delegate. In VB2005, it is also little tedious to do all the type casting when Remove, RemoveAll and Combine are used. Hopefully, Cobra can simply that.
Then comes the event concept with reserved words:
- Code: Select all
Event, WithEvent, Handles, AddHandler, RemoveHandler, AddressOf, RaiseEvent
The Event object has to be declared as
- Code: Select all
Public Event BehaviourEvent()
The object with an event is usually declared something like:
- Code: Select all
Dim WithEvents aButton As New System.Windows.Forms.Button
But it is also possible to declare a button object without the WithEvents keyword as. The difference seems to be that for a aButton object declared with the WithEvent keyword, the compiler adds all methods marked with the Handles keyword to the list belonging to the appropriate aButton Event object:
- Code: Select all
Sub SomeBehaviour Handles aButton.Click
I don’t know why it should be possible to inhibit this by declaring an object without the WithEvent keyword. So I think the WithEvent keyword is not necessary.
The AddHandler, RemoveHandler used with the AddressOf keyword seems syntactic sugar for the delegate methods. I believe that normal collection methods are easier understood and used (but not the present ,NET delegate methods).
The keyword RaiseEvent that executes the methods in the method list connected to the Click event in VB:
- Code: Select all
RaiseEvent Click
And RaiseEvent can only be used IN THE SAME method or class that has the event declaration! Why? Delegates don’t have this restriction, and why isn’t the delegate invoke method used instead? This limitation has also given the convention in some places that the method added to the event list should be named On<EventName>.
From a black box perspective, the Event class seems to be a wrapper around the Delegate class. The main advantage with the event concept seems to be that it extends the delegate concept with Handles, so that methods are added automatically to the delegate list by the compiler at compile time. But the restriction with RaiseEvent limits the events usability compared with delegates.
I would like that functions in Cobra by default have some delegate functionality so it would be possible to write straight forwardly:
- Code: Select all
anObject.Behaviour = modifiedBehaviour
It should used anywhere just like present VB delegates:
- Code: Select all
anObject.Behaviour
For this, the Cobra delegate should have a subset of the present .NET delegate functionality since each Cobra delegate should have just one method connected to it, not a list of methods. Hopefully, Raise in the Cobra event concept doesn’t have the VB RaiseEvent limitations.
Hopefully, this clarifies what I dislike about delegates and events in VB.
Regards
Csaba