<?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 continued.. ByRef, ByVal and ()</title><link>http://msmvps.com/blogs/bill/archive/2006/04/08/89902.aspx</link><description>continuing the discussion on closures …. A rarely used, somewhat obscure feature of VB, is by enclosing a variable in ()'s it is passed ByVal instead of ByRef. Consider the following code: (1) Dim s As String = "hello" Foo(s) Console.Writeline(s) versus</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Parenthesized expressions (more on Closures)</title><link>http://msmvps.com/blogs/bill/archive/2006/04/08/89902.aspx#92285</link><pubDate>Mon, 24 Apr 2006 04:26:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:92285</guid><dc:creator>@ Head</dc:creator><description>since Scott said such nice things about my last post on closures, I thought I'd add some quick references...&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=92285" width="1" height="1"&gt;</description></item><item><title>re: closures continued.. ByRef, ByVal and ()</title><link>http://msmvps.com/blogs/bill/archive/2006/04/08/89902.aspx#90353</link><pubDate>Tue, 11 Apr 2006 01:52:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:90353</guid><dc:creator>bill</dc:creator><description>Let me address this question first:&lt;br&gt;&lt;br&gt;&amp;gt; It also raises the question of what (x + y)&lt;br&gt;&amp;gt; would do -- would that uncapture both x and y? &lt;br&gt;&lt;br&gt;I think what you are sayign there is actually much of what I was sayign, in that we need to move away from the use of just ()'s.  If you adopted the (ByVal o) syntax there could not be that ambiguity.  &lt;br&gt;You couldn't have (ByVal x + y).&lt;br&gt;It would have to be (ByVal x) + y&lt;br&gt;Syntatx like (ByVal (x + y)) would be illegal because x + y is an expression that produces a r value already.&lt;br&gt;&lt;br&gt;I definetly disagree on the idea it should be r values instead of l values by default.  That's jsut going to make it more confussing.  I'd like to hear your arguements why you think it should be r. I presented some in my previous blog entry on captures but came to the conclussion that l values were preferable.&lt;br&gt;&lt;br&gt;As to the parens with the capture (that is parens with the ByVal keyword added &amp;lt;g&amp;gt;), I think it does actually make a difference in what the compiler can do.  The ()'s means a temporary variable is created, and as such the VB compiler can treat the compiler created variable differently from the code written variable, allowing things such as escaping out of the issue of the local variables insid a loop.  Because the variable is a compiler generated one, you'd know there is no need ot link that to the original code, rather you'd use it to instantiate a value in the anon delegate's generated class.&lt;br&gt;&lt;br&gt;to me, the (ByVal o) means the same as *ptr instead of **ptr in the effect it has on the code.  &lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=90353" width="1" height="1"&gt;</description></item><item><title>re: closures continued.. ByRef, ByVal and ()</title><link>http://msmvps.com/blogs/bill/archive/2006/04/08/89902.aspx#90346</link><pubDate>Tue, 11 Apr 2006 01:21:46 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:90346</guid><dc:creator>Paul Vick</dc:creator><description>I think you're confusing two different things. In the case of expressions being passed to reference parameters, the real feature is that we allow you to pass r-values to reference parameters by creating a temporary to hold the value. This allows you to pass (x) to a reference parameter, but it also allows you to pass the literal 10 to a reference parameter. There's really nothing special about the parenthesis except that they, by their nature, always produce a r-value.&lt;br&gt;&lt;br&gt;The parens don't help you in the closure case because what's being captured is the actual variable reference itself. How that variable reference gets used in an encompassing expression has no bearing on the capture, so it would actually be weird (from an internal consistency standpoint) to have parenthesis somehow &amp;quot;uncapture&amp;quot; the l-value within it. It also raises the question of what (x + y) would do -- would that uncapture both x and y?&lt;br&gt;&lt;br&gt;All that being said, I don't totally disagree with the idea of having an explicit way of switching between the capture semantics, although I might argue the default should be to just capture r-values and then have an opt-in way for capturing l-values (i.e. a ByRef modifier rather than a ByVal modifier). Still, that's adding another layer of complexity, so I can't really say that we'll do something here, but it's definitely something we'll talk about...&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=90346" width="1" height="1"&gt;</description></item><item><title>re: closures continued.. ByRef, ByVal and ()</title><link>http://msmvps.com/blogs/bill/archive/2006/04/08/89902.aspx#90197</link><pubDate>Mon, 10 Apr 2006 03:59:06 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:90197</guid><dc:creator>Jonathan Allen</dc:creator><description>That syntax looks pretty clean to me, and is a heck of a lot better than what we have now.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=90197" width="1" height="1"&gt;</description></item></channel></rss>