.NET

Reply
Mentor
Amremad
Posts: 216
Registered: ‎08-24-2010
Message 1 of 3 (260 Views)

Event & delegate

260 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
        CreatePaletteSet()
        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 !!!!

Mentor
khoa.ho
Posts: 209
Registered: ‎09-15-2011
Message 2 of 3 (237 Views)

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.

-Khoa
www.netscriptcad.com

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 3 of 3 (227 Views)

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).

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.