{"id":5913,"date":"2007-06-15T17:04:00","date_gmt":"2007-06-15T17:04:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2007\/06\/15\/closures-in-vb-part-4-variable-lifetime\/"},"modified":"2024-07-05T14:45:00","modified_gmt":"2024-07-05T21:45:00","slug":"closures-in-vb-part-4-variable-lifetime","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/closures-in-vb-part-4-variable-lifetime\/","title":{"rendered":"Closures in VB Part 4: Variable Lifetime"},"content":{"rendered":"<p>For previous articles in this series please see<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.msdn.com\/vbteam\/archive\/2007\/05\/02\/closures-in-vb-part-1.aspx\">Part 1: Introduction<\/a>\n<li><a href=\"http:\/\/blogs.msdn.com\/vbteam\/archive\/2007\/05\/03\/closures-in-vb-part-2-method-calls.aspx\">Part 2: Method Calls<\/a>\n<li><a href=\"http:\/\/blogs.msdn.com\/vbteam\/archive\/2007\/05\/25\/closures-in-vb-part-3-scope.aspx\">Part 3: Scope<\/a><\/li>\n<\/ul>\n<p>Sorry for the long delay between posts here.&nbsp; We&#8217;re getting Orcas out the door and getting this series completed takes a back seat to shipping.&nbsp; <\/p>\n<p>Originally I wanted to talk about looping structures next.&nbsp; However when I started writing that post I realized that I had to talk about lifetime before the looping structures would make sense.&nbsp; <\/p>\n<p>Prior to Orcas the lifetime of a variable in VB was the entire function.&nbsp; This presented several problems from a closures perspective.&nbsp; Imagine you had a looping structure and the value was used in a lambda expression.&nbsp; <\/p>\n<pre>    <span>Sub<\/span> LifetimeExample()\n        <span>Dim<\/span> list <span>As<\/span> <span>New<\/span> List(Of Func(Of <span>Integer<\/span>))\n        <span>For<\/span> i = <span>0<\/span> <span>To<\/span> <span>5<\/span>\n            <span>Dim<\/span> x = i * <span>2<\/span>\n            <span>If<\/span> <span>True<\/span> <span>Then<\/span>\n                list.Add(<span>Function<\/span>() x)\n            <span>End<\/span> <span>If<\/span>\n        <span>Next<\/span>\n        <span>For<\/span> <span>Each<\/span> f <span>In<\/span> list\n            Console.Write(f() &amp; \" \")\n        <span>Next<\/span>\n    <span>End<\/span> <span>Sub<\/span><\/pre>\n<p>In this example if we left the lifetime rules unchanged, there would be a single variable &#8220;x&#8221; for the entire function.&nbsp; That means that we would end up printing out <\/p>\n<p>10 10 10 10 10<\/p>\n<p>This is somewhat unexpected and essentially means that VB could not support complex Lambda scenarios.&nbsp; To fix this we altered the lifetime of variables to be tied to the scope they were contained in.&nbsp; The end effect is that each iteration of the loop has a separate &#8220;x&#8221; since each iteration enters and leaves the scope of the &#8220;if&#8221; statement.&nbsp; As a result it will print out <\/p>\n<p>0 2 4 6 8 10<\/p>\n<p>We did make one backcompat adjustment for this change.&nbsp; The lifetime of variables in VB was visible if you tried to use an uninitialized variable in a loop\/goto.&nbsp; For instance the following code will also print out 0 2 4 6 8 10 because it takes advantage of the fact that the variable &#8220;x&#8221; has a lifetime longer than the loop.<\/p>\n<pre>    <span>Sub<\/span> VisibleLifetime()\n        <span>For<\/span> i = <span>0<\/span> <span>To<\/span> <span>5<\/span>\n            <span>Dim<\/span> x <span>As<\/span> <span>Integer<\/span>\n            Console.WriteLine(x)\n            x += <span>2<\/span>\n        <span>Next<\/span>\n    <span>End<\/span> <span>Sub<\/span><\/pre>\n<p>To make sure that we didn&#8217;t break any existing code we had one little errata for the change.&nbsp; When a variable&#8217;s scope is re-entered, and hence recreated, and it is not initialized to a value it will get the last value of the variable.&nbsp;<\/p>\n<p>Jared Parsons (<a href=\"http:\/\/blogs.msdn.com\/jaredpar\">http:\/\/blogs.msdn.com\/jaredpar<\/a>&nbsp;)&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For previous articles in this series please see Part 1: Introduction Part 2: Method Calls Part 3: Scope Sorry for the long delay between posts here.&nbsp; We&#8217;re getting Orcas out the door and getting this series completed takes a back seat to shipping.&nbsp; Originally I wanted to talk about looping structures next.&nbsp; However when I [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[78,117],"class_list":["post-5913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-jared-parsons","tag-orcas"],"acf":[],"blog_post_summary":"<p>For previous articles in this series please see Part 1: Introduction Part 2: Method Calls Part 3: Scope Sorry for the long delay between posts here.&nbsp; We&#8217;re getting Orcas out the door and getting this series completed takes a back seat to shipping.&nbsp; Originally I wanted to talk about looping structures next.&nbsp; However when I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/5913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=5913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/5913\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=5913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=5913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=5913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}