.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Event & delegate

2 REPLIES 2
Reply
Message 1 of 3
Amremad
634 Views, 2 Replies

Event & delegate

    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 !!!!

2 REPLIES 2
Message 2 of 3
khoa.ho
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

Message 3 of 3
DiningPhilosopher
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).

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost