<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Event &amp;amp; delegate in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/event-amp-delegate/m-p/3882513#M49883</link>
    <description>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.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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).&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Tue, 23 Apr 2013 17:07:59 GMT</pubDate>
    <dc:creator>DiningPhilosopher</dc:creator>
    <dc:date>2013-04-23T17:07:59Z</dc:date>
    <item>
      <title>Event &amp; delegate</title>
      <link>https://forums.autodesk.com/t5/net-forum/event-amp-delegate/m-p/3878539#M49881</link>
      <description>&lt;PRE&gt;    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&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    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&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;above codes do the same setting&lt;/P&gt;&lt;P&gt;so delegate's advantages to use it , i think the event is better than delegate in this method&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any explain !!!!&lt;/P&gt;</description>
      <pubDate>Tue, 23 Apr 2013 08:55:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/event-amp-delegate/m-p/3878539#M49881</guid>
      <dc:creator>Amremad</dc:creator>
      <dc:date>2013-04-23T08:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: Event &amp; delegate</title>
      <link>https://forums.autodesk.com/t5/net-forum/event-amp-delegate/m-p/3882323#M49882</link>
      <description>&lt;P&gt;The comparison between AutoCAD event vs delegate can be narrow down to the following code:&lt;BR /&gt;&lt;BR /&gt;1) db.ObjectAppended += (sender, args) =&amp;gt; { };&lt;BR /&gt;2) db.ObjectAppended += delegate(object sender, ObjectEventArgs args) { };&lt;BR /&gt;3) db.ObjectAppended += new ObjectEventHandler(DBOnObjectAppended);&lt;BR /&gt;4) db.ObjectAppended += DBOnObjectAppended;&lt;BR /&gt;&lt;BR /&gt;My explanation to simplify the concept:&lt;BR /&gt;&lt;BR /&gt;1. Lambda expression which is a shorter form of delegate, an anonymous delegate.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;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.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;3. Create a new delegate object ObjectEventHandler, with passing parameter is a real function. Here is an implementation:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;public delegate void ObjectEventHandler(object sender, ObjectEventArgs e);&lt;BR /&gt;db.ObjectAppended += new ObjectEventHandler(DBOnObjectAppended);&lt;BR /&gt;private static void DBOnObjectAppended(object sender, ObjectEventArgs objectEventArgs)&lt;BR /&gt;{&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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);&lt;BR /&gt;&lt;BR /&gt;4. Just a shorter form of 3, hide new ObjectEventHandler delegate.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;-Khoa&lt;BR /&gt;&lt;A target="_blank" href="http://www.netscriptcad.com"&gt;www.netscriptcad.com&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Apr 2013 14:50:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/event-amp-delegate/m-p/3882323#M49882</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2013-04-23T14:50:31Z</dc:date>
    </item>
    <item>
      <title>Re: Event &amp; delegate</title>
      <link>https://forums.autodesk.com/t5/net-forum/event-amp-delegate/m-p/3882513#M49883</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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).&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 23 Apr 2013 17:07:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/event-amp-delegate/m-p/3882513#M49883</guid>
      <dc:creator>DiningPhilosopher</dc:creator>
      <dc:date>2013-04-23T17:07:59Z</dc:date>
    </item>
  </channel>
</rss>

