217 Posts
1 Kudo
Registered: ‎08-24-2010
Post 1 of 3

Event & delegate

295 Views, 2 Replies
04-23-2013 01:55 AM
    Dim WithEvents curDwg1 As Database = Application.DocumentManager.MdiActiveDocument.Database
    Private Sub curDwg1_ObjectAppended(sender As Object, e As ObjectEventArgs) Handles curDwg1.ObjectAppended
        Dim NewNode As System.Windows.Forms.TreeNode = MyPalette.TreeView1.Nodes.Add(e.DBObject.GetType().ToString())
        NewNode.Tag = e.DBObject.ObjectId.ToString()
    End Sub


    Private Sub AddHanddle()
        Dim curDwg As Database = Application.DocumentManager.MdiActiveDocument.Database
        AddHandler curDwg.ObjectAppended, New ObjectEventHandler(AddressOf callback_ObjectAppended)
    End Sub

    Private Sub callback_ObjectAppended(ByVal sender As Object, ByVal e As ObjectEventArgs)
        Dim NewNode As System.Windows.Forms.TreeNode = MyPalette.TreeView1.Nodes.Add(e.DBObject.GetType().ToString())
        NewNode.Tag = e.DBObject.ObjectId.ToString()
    End Sub

 above codes do the same setting

so delegate's advantages to use it , i think the event is better than delegate in this method


any explain !!!!

225 Posts
24 Kudos
Registered: ‎09-15-2011
Post 2 of 3

Re: Event & delegate

04-23-2013 07:50 AM in reply to: Amremad

The comparison between AutoCAD event vs delegate can be narrow down to the following code:

1) db.ObjectAppended += (sender, args) => { };
2) db.ObjectAppended += delegate(object sender, ObjectEventArgs args) { };
3) db.ObjectAppended += new ObjectEventHandler(DBOnObjectAppended);
4) db.ObjectAppended += DBOnObjectAppended;

My explanation to simplify the concept:

1. Lambda expression which is a shorter form of delegate, an anonymous delegate.

2. Create a new delegate, which is a template function, with parameters but without implementation. So we have to put our code between { } for implementation.

3. Create a new delegate object ObjectEventHandler, with passing parameter is a real function. Here is an implementation:

public delegate void ObjectEventHandler(object sender, ObjectEventArgs e);
db.ObjectAppended += new ObjectEventHandler(DBOnObjectAppended);
private static void DBOnObjectAppended(object sender, ObjectEventArgs objectEventArgs)

Event handler is just a delegate type, which represents any function with the same parameters but different implementation. The way an event db.ObjectAppended works is that whenever the database is appended with a new object, it will call the delegate function, which can be seen as a “function variable”, similar to normal object variables.

Delegate comes with the namespace, so whenever we use “using Autodesk.AutoCAD.DatabaseServices”, public delegate void ObjectEventHandler(object sender, ObjectEventArgs e); is included. Similar to “using System;” will include the common EventHandler delegate. Therefore, on AutoCAD, just use db.ObjectAppended += new ObjectEventHandler(DBOnObjectAppended);

4. Just a shorter form of 3, hide new ObjectEventHandler delegate.

So event and delegate are the same to trigger an event, but event makes more human sense to trigger an action when something happens. The difference is that event adds a layer of abstraction and protection on the delegate instance.


Valued Mentor
370 Posts
175 Kudos
Registered: ‎05-06-2012
Post 3 of 3

Re: Event & delegate

04-23-2013 10:07 AM in reply to: Amremad
When you use 'WithEvents' keyword, the VB.NET compiler generates code that does the same as your delegate version, only it does it for you.

You can see the generated code in Reflector, but there is one very important difference, which is that it if you assign a different value to the variable that holds the event source, the handler will be removed from the previous value and added to the new value, automatically by the compiler-generated code.

If you use WithEvents with a document variable, and you keep changing the value of the Document variable, each time you do that, the code that is generated by the VB Compiler will remove the event handler from the previous value of the variable, and add it to the new value (if not Nothing).

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!