<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx</link><description>A recent discussion on Paul's blog about local variables scope and lifetime led the discussion to that of closures. So maybe this is a good time to look at closures in general…. Personally, I prefer to think of "closures" as being "state machines". That</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Closures in VB.Net</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#489604</link><pubDate>Sat, 13 Jan 2007 20:23:42 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:489604</guid><dc:creator>Ayende @ Blog</dc:creator><description>&lt;p&gt;Closures in VB.Net&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=489604" width="1" height="1"&gt;</description></item><item><title>My and threading (TheadStatic, ThreadSafeObjectProvider)</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#90920</link><pubDate>Fri, 14 Apr 2006 06:59:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:90920</guid><dc:creator>@ Head</dc:creator><description>Yesterday I got an interesting email from Tobin Titus about an issue with threading and My.Forms, so...&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=90920" width="1" height="1"&gt;</description></item><item><title>My and threading (TheadStatic, ThreadSafeObjectProvider)</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#90919</link><pubDate>Fri, 14 Apr 2006 06:50:08 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:90919</guid><dc:creator>@ Head</dc:creator><description>Yesterday I got an interesting email from Tobin Titus about an issue with threading and My.Forms, so...&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=90919" width="1" height="1"&gt;</description></item><item><title>closures continued.. ByRef, ByVal and ()</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89903</link><pubDate>Sun, 09 Apr 2006 02:35:18 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89903</guid><dc:creator>@ Head</dc:creator><description>continuing the discussion on closures….&lt;br&gt;&amp;amp;#160;&lt;br&gt;A rarely used, somewhat obscure feature of VB, is by enclosing...&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89903" width="1" height="1"&gt;</description></item><item><title>re: Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89888</link><pubDate>Sat, 08 Apr 2006 22:57:10 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89888</guid><dc:creator>Jonathan Allen</dc:creator><description>&amp;gt; Have you downloaded the VB LINQ preview bits ? &lt;br&gt;&lt;br&gt;Yep, but I haven't had time to properly explore them.&lt;br&gt;&lt;br&gt;Thanks for the info, it makes sense now.&lt;br&gt;&lt;br&gt;&amp;gt; I could see ByVal being handy in places just like how we use to use ()'s in Vb6 and earlier. (note I would NOT want to see the return of that behavior by using ()'s alone)  I could also see using ByVal would be handy when passing a Property into a method where the parameter is ByRef.&lt;br&gt;&lt;br&gt;According to the docs, (var) is still the way to force it to be ByVal. I don't like that. I would perfer it to be...&lt;br&gt;&lt;br&gt;foo( byval x)&lt;br&gt;    or&lt;br&gt;Dim del as New SimpleDelegate(){Console.WriteLine(ByVal person.Name)}&lt;br&gt;&lt;br&gt;But upon looking at it, I don't think that will work for the delegate. There is no way to determine if person or person.Name is being passed by value. I guess ByVal(person).Name and ByVal(Person.Name) will have to be used.&lt;br&gt;&lt;br&gt;Ok, I think I agree with your main points. I would like to say we only need the ByVal keyword, but we might as well have both to make it easier on people reading the code.&lt;br&gt;&lt;br&gt;For the default case (I assume it would be ByRef), should that be explicit? In other words, should the IDE automatically add ByRef?&lt;br&gt;&lt;br&gt;I'm leaning towards no, as it may clutter the code too much.&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89888" width="1" height="1"&gt;</description></item><item><title>re: Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89821</link><pubDate>Sat, 08 Apr 2006 07:33:09 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89821</guid><dc:creator>bill</dc:creator><description>Oh, okay.  Have you downloaded the VB LINQ preview bits ?  That's probably the best way to get an understanding of the &amp;quot;why&amp;quot;'s in relation to query comprehensions and lambda expressions. Perhaps the following might help to explain:&lt;br&gt;&lt;br&gt;consider a projection where you want to slect a group of people based on some of their attributes.  Purely theoretical, but lets assume they have a DateOfBirth property, and you want ot get those people's who birthday it is for a given day.  So you write the projection, and you feed it the date you are interested in when you iterate the projection.&lt;br&gt;This lets you re-use the same projection.&lt;br&gt;&lt;br&gt;Now as part of that query comprehension that gives you the projection, you need lambda expression to deal with getting the person's DOB, then comparing the month and day values against the given date.  Obviously that can't occur at compile time. So the vlaues for the expression are dynamic, they are for each person.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89821" width="1" height="1"&gt;</description></item><item><title>re: Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89804</link><pubDate>Sat, 08 Apr 2006 03:12:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89804</guid><dc:creator>Jonathan Allen</dc:creator><description>No, that would be silly.&lt;br&gt;&lt;br&gt;I am under the impression that lambda expressions and anonymous delegates are essential for LINQ, but I don't know why. Nor do I know whether (or should I say when) ByVal or ByRef behaviour is more apropriate.&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89804" width="1" height="1"&gt;</description></item><item><title>re: Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89665</link><pubDate>Fri, 07 Apr 2006 07:24:58 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89665</guid><dc:creator>bill</dc:creator><description>I don't understand your question. LINQ statements can contain lambda expressions and anonymous delegates. Surely you aren't suggesting the behaviour of a nested function be different when used in a LINQ statement compared to the surrounding code ?&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89665" width="1" height="1"&gt;</description></item><item><title>re: Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89638</link><pubDate>Fri, 07 Apr 2006 05:50:58 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89638</guid><dc:creator>Jonathan Allen</dc:creator><description>Ok, I buy the nested functions argument.&lt;br&gt;&lt;br&gt;In the terms of LINQ, what do we gain by having the &amp;quot;ByRef&amp;quot; behaviour?&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89638" width="1" height="1"&gt;</description></item><item><title>re: Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89619</link><pubDate>Fri, 07 Apr 2006 00:25:19 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89619</guid><dc:creator>bill</dc:creator><description>the declaration of del defines it's relationship. That is no different for any method or delegate.  &lt;br&gt;&lt;br&gt;You need to see anonymous delegates like nested procedures to see the forest. &lt;br&gt;&lt;br&gt;Let me dilute the example a bit and change it to a class.....&lt;br&gt;&lt;br&gt;Class MyClass&lt;br&gt;&lt;br&gt;   Private person As Person&lt;br&gt;&lt;br&gt;   Sub PrintReport()&lt;br&gt;      Console.WriteLine(person.Name)&lt;br&gt;   End sub&lt;br&gt;&lt;br&gt;   Sub Test()&lt;br&gt;     person = New Person(&amp;quot;Fred&amp;quot;) &lt;br&gt;     PrintReport() &lt;br&gt;     person = New Person(&amp;quot;Wilma&amp;quot;) &lt;br&gt;     PrintReport() &lt;br&gt;   End sub&lt;br&gt;&lt;br&gt;End Class&lt;br&gt;&lt;br&gt;So calling the Test method inside an instance of MyClass would print out Fred and Wilma.  That is the variables of the container are shared amongst the procedures by reference.&lt;br&gt;&lt;br&gt;Now picture anonymous delegates as nested procedures. Their container is the procedure in which they are defined.  It's like fractals I suppose, we are just moving the level of encapsulation one level finer.&lt;br&gt;        &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89619" width="1" height="1"&gt;</description></item><item><title>re: Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89566</link><pubDate>Thu, 06 Apr 2006 15:39:43 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89566</guid><dc:creator>Jonathan Allen</dc:creator><description>person = New Person(&amp;quot;Fred&amp;quot;) &lt;br&gt;del() &lt;br&gt;person = New Person(&amp;quot;Wilma&amp;quot;) &lt;br&gt;del() &lt;br&gt;&lt;br&gt;There is no indication that the result of the function del is in anyway related to changing the value of person. As such, I would consider using the anonymous delegate in this fashion to be improper.&lt;br&gt;&lt;br&gt;Can you give me an example of who you would use it in a real project?&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89566" width="1" height="1"&gt;</description></item><item><title>re: Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89512</link><pubDate>Wed, 05 Apr 2006 23:42:20 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89512</guid><dc:creator>bill</dc:creator><description>Jonathan, I think it really depends on how you view anonymous delegates and lambda expressions. If you view them as just inline function calls, then you don't get the beauty and power of them as re-usable code blocks. Perhaps the easiest way is for people to think of them as nested procedures. &lt;br&gt;&lt;br&gt;I think as soon as you start doing anything with them other than very simple statements, you do expect them to be more &amp;quot;ByRef&amp;quot;. Let's expand the sample given above: &lt;br&gt;&lt;br&gt;Dim person As Person &lt;br&gt;&lt;br&gt;Dim del as New SimpleDelegate(){Console.WriteLine(person.Name)} &lt;br&gt;&lt;br&gt;person = New Person(&amp;quot;Fred&amp;quot;) &lt;br&gt;del() &lt;br&gt;person = New Person(&amp;quot;Wilma&amp;quot;) &lt;br&gt;del() &lt;br&gt;person = New Person(&amp;quot;Barney&amp;quot;) &lt;br&gt;del() &lt;br&gt;person = New Person(&amp;quot;Betty&amp;quot;) &lt;br&gt;del() &lt;br&gt;&lt;br&gt;Now would you expect that to print out &amp;quot;Fred&amp;quot;, &amp;quot;Fred&amp;quot;, &amp;quot;Fred&amp;quot;, &amp;quot;Fred&amp;quot;, or the cast of characters ? &lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89512" width="1" height="1"&gt;</description></item><item><title>re: Closures… Implicit or Explicit ??</title><link>http://msmvps.com/blogs/bill/archive/2006/04/05/89370.aspx#89492</link><pubDate>Wed, 05 Apr 2006 20:21:02 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:89492</guid><dc:creator>Jonathan Allen</dc:creator><description>I'm leaning in your direction, but you haven't convienced me yet. Can you give some more examples of when the &amp;quot;ByRef&amp;quot; behaviour is desirable?&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=89492" width="1" height="1"&gt;</description></item></channel></rss>