<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>The Visual Basic Team : Jared Parsons</title><link>http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx</link><description>Tags: Jared Parsons</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Community Interview with Jared Parsons (Beth Massi)</title><link>http://blogs.msdn.com/vbteam/archive/2008/10/15/community-interview-with-jared-parsons-beth-massi.aspx</link><pubDate>Wed, 15 Oct 2008 19:00:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8974119</guid><dc:creator>VBTeam</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/8974119.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=8974119</wfw:commentRss><description>&lt;p&gt;Here's another &lt;a href="http://blogs.msdn.com/vbteam/archive/tags/Interview/default.aspx" target="_blank"&gt;community interview&lt;/a&gt; from our Italian Visual Basic MVP &lt;a href="http://community.visual-basic.it/Alessandro/" target="_blank"&gt;Alessandro Del Sole&lt;/a&gt;, this time with Jared Parsons, a Developer on the VB Team. Jared is a great developer and loves the community. You can check out his &lt;a href="http://blogs.msdn.com/jaredpar" target="_blank"&gt;personal blog here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;You can also check out the &lt;a href="http://msdn.microsoft.com/en-us/vbasic/bb735849.aspx" target="_blank"&gt;Visual Basic Team page for pictures&lt;/a&gt; and bios and visit the &lt;a href="http://msdn.microsoft.com/en-us/vbasic/default.aspx" target="_blank"&gt;home page of the Visual Basic Developer Center&lt;/a&gt; for a link to all &lt;a href="http://blogs.msdn.com/vbteam/archive/tags/Interview/default.aspx" target="_blank"&gt;these interviews&lt;/a&gt; as well.&lt;/p&gt;  &lt;p&gt;Here's the interview with Jared: &lt;/p&gt;  &lt;p&gt;1. Nice to meet you Jared Parsons! Let&amp;#8217;s begin by asking a simple question: where are you from?&lt;/p&gt;  &lt;p&gt;Born in Kentucky, raised in Tennessee, lived in Alabama for awhile, then back to Tennessee and went to college in Georgia. Usually I simplify it by saying I grew up all over the southeast. &lt;/p&gt;  &lt;p&gt;2. We all know that you&amp;#8217;re a Visual Basic Team member, but could we know something more about your role inside the Team?&lt;/p&gt;  &lt;p&gt;I&amp;#8217;m a developer in the VB team. I started out on the VB Compiler several years ago. Towards the end of Visual Studio 2008 I switched to the VB expression evaluator (aka debugger, watch, immediate window). This role expanded to other parts of the VB IDE experience including a lot of features users will be (hopefully) really excited about for the next release of Visual Studio.&lt;/p&gt;  &lt;p&gt;3. What&amp;#8217;s your favorite Visual Basic feature regarding the language and/or the IDE?&lt;/p&gt;  &lt;p&gt;My favorite VB feature is usually whatever I&amp;#8217;m implementing at the moment I&amp;#8217;m asked this questionJ. Luckily I&amp;#8217;m in between features at the moment so I can give an unbiased answer. XML Literals are my favorite VB feature. It really allows me to seamlessly integrate my data and queries into my application. I feel like I find a new way to use it every day.&lt;/p&gt;  &lt;p&gt;4. How long have you been working for Microsoft and what was your occupation before joining the VB Team?&lt;/p&gt;  &lt;p&gt;I&amp;#8217;ve been working at Microsoft for 4 &amp;#189; years now. I joined Microsoft immediately after college in the in the Visual Studio Team System division. More specifically I was a developer on Team Developer and Team Architect. After shipping Visual Studio 2005 I was eager for new challenges. I spent a good deal of my time in college working on low level systems and being a teaching assistant for an introduction to compiler courses. I was interested in getting back to these roots. A job opened up on the VB compiler team and it was the perfect fit.&lt;/p&gt;  &lt;p&gt;5. Why do you think VB is such a great language?&lt;/p&gt;  &lt;p&gt;We have a tie here: flexibility and IDE. VB is an amazingly flexible language. It lends itself to whatever task I&amp;#8217;m trying to do. On one end of the spectrum I have XML literals to work with my data and at the other end generics and lambda expressions to really geek out on a problem. The IDE support is also very good (I promise I&amp;#8217;m not biased here). It&amp;#8217;s really difficult to express everything the IDE offers to the programmer. The best way to understand though is to get out notepad and try and code up a VB application. That will really give you a handle on what the IDE is doing for you.&lt;/p&gt;  &lt;p&gt;6. Have you ever been to Italy?&lt;/p&gt;  &lt;p&gt;No but it&amp;#8217;s definitely on the list of places to visit.&lt;/p&gt;  &lt;p&gt;7. Would you like to tell us something about your life outside the office (how do you like to spend your free time, what do you like to do when/if you go out in the evening or when you&amp;#8217;re at home and so on)?&lt;/p&gt;  &lt;p&gt;Outside of work I still lead a largely geek lifestyle; lots of programming, blogging and book reading. I do manage to leave my geek role by playing a variety of sports. Right now I&amp;#8217;m playing soccer (indoor and outdoor), softball and football. I&amp;#8217;m not very picky when it comes to sports. As long as it&amp;#8217;s athletic and preferably outdoors I&amp;#8217;m there. It&amp;#8217;s a great way to get release after a day of programming.&lt;/p&gt;  &lt;p&gt;Of course there is the occasional Rock Band party as well.&lt;/p&gt;  &lt;p&gt;8. Who is your favorite musician?&lt;/p&gt;  &lt;p&gt;Much to my parent&amp;#8217;s disappointment, I don&amp;#8217;t have an ear for music (read that as 100% tone deaf). As a consequence my taste for music is &amp;#8230; bad according to my friends. I prefer to think of it as blissful ignorance. On that note (pun intentional) I love a good punk album or some techno while coding. &lt;/p&gt;  &lt;p&gt;9. What&amp;#8217;s your favorite food?&lt;/p&gt;  &lt;p&gt;The hands down winner here is French Fries.&lt;/p&gt;  &lt;p&gt;Thank you very much for taking time to answer these questions and letting us know something more about you!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8974119" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/Beth+Massi/default.aspx">Beth Massi</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Interview/default.aspx">Interview</category></item><item><title>VB Catch ... When: Why so special?</title><link>http://blogs.msdn.com/vbteam/archive/2008/10/09/vb-catch-when-why-so-special.aspx</link><pubDate>Thu, 09 Oct 2008 18:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8990906</guid><dc:creator>VBTeam</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/8990906.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=8990906</wfw:commentRss><description>&lt;P&gt;The VB Catch syntax has a unique feature: When.&amp;nbsp; It allows users to filter expressions based on something other than their type.&amp;nbsp; Any arbitrary code can enter a When block to decide whether or not to handle an Exception.&lt;/P&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: blue"&gt;Sub &lt;/SPAN&gt;Sub1()
        &lt;SPAN style="COLOR: blue"&gt;Try
            &lt;/SPAN&gt;DoSomeAction()
        &lt;SPAN style="COLOR: blue"&gt;Catch &lt;/SPAN&gt;ex &lt;SPAN style="COLOR: blue"&gt;As &lt;/SPAN&gt;Exception &lt;SPAN style="COLOR: blue"&gt;When &lt;/SPAN&gt;Filter(ex)
            &lt;SPAN style="COLOR: blue"&gt;Stop
        End Try
    End Sub
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Newsgroups often ask, "Why's this so special? I could effectively get the same behavior out of C# by doing the following:"&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;static void &lt;/SPAN&gt;Sub1()
{
    &lt;SPAN style="COLOR: blue"&gt;try
    &lt;/SPAN&gt;{
        DoSomeAction();
    }
    &lt;SPAN style="COLOR: blue"&gt;catch &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Exception &lt;/SPAN&gt;ex)
    {
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(Filter(ex))
        {
            &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt;;
        }
        HandleException();
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This is true to an extent.&amp;nbsp; In both cases the code is handling an exception and making a decision, via calling Filter, as to whether or not to handle the exception.&amp;nbsp; The subtle difference is when the Filter method is called.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;In VB the When statement is actually implemented as an IL exception filter.&amp;nbsp; When an exception is thrown, exception filters are processed before the stack is unwound.&amp;nbsp; This means that if the Filter method created an error report that included the current stack trace, it would show the frame in which the exception occurred.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;For example, in the code above if DoSomeAction() threw and the stack was examined in the Filter expression, the following stack would show up. &lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/VBCatch.WhenWhysospecial_1299E/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/VBCatch.WhenWhysospecial_1299E/image_2.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=304 alt=image src="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/VBCatch.WhenWhysospecial_1299E/image_thumb.png" width=504 border=0 mce_src="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/VBCatch.WhenWhysospecial_1299E/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Notice how the DoSomeAction method is clearly visible?&amp;nbsp; This is incredibly powerful for features like error reporting and investigation.&amp;nbsp; It also allows you to set powerful breakpoints where the exact state of the error can be examined and not just the post mortem.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Alternatively, code executed in the C# block will occur after the stack is unwound.&amp;nbsp; As long as you're not in optimized code you can usually use the stack trace properties to get the source of the exception, but you won't be able to examine the live state of the error. &lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/VBCatch.WhenWhysospecial_1299E/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/VBCatch.WhenWhysospecial_1299E/image_4.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=304 alt=image src="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/VBCatch.WhenWhysospecial_1299E/image_thumb_1.png" width=501 border=0 mce_src="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/VBCatch.WhenWhysospecial_1299E/image_thumb_1.png"&gt;&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8990906" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/VB2005/default.aspx">VB2005</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/VB2008/default.aspx">VB2008</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Did+you+know_3F00_/default.aspx">Did you know?</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/VB2010/default.aspx">VB2010</category></item><item><title>Channel 9 Interview: Jared Parsons on the P-Invoke Interop Assistant (Beth Massi)</title><link>http://blogs.msdn.com/vbteam/archive/2008/07/28/channel-9-interview-jared-parsons-on-the-p-invoke-interop-assistant-beth-massi.aspx</link><pubDate>Tue, 29 Jul 2008 04:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8785968</guid><dc:creator>VBTeam</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/8785968.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=8785968</wfw:commentRss><description>&lt;SPAN id=ctl00_MainPlaceHolder_Starter_BodyLabel&gt;&lt;A class="" href="http://channel9.msdn.com/posts/funkyonex/The-P-Invoke-Interop-Assistant/" target=_blank mce_href="http://channel9.msdn.com/posts/funkyonex/The-P-Invoke-Interop-Assistant/"&gt;Today I posted an interview screencast&amp;nbsp;on Channel 9&lt;/A&gt;.&amp;nbsp;In this interview,&amp;nbsp;&lt;A href="http://blogs.msdn.com/jaredPar"&gt;Jared Parsons&lt;/A&gt;, a Developer on the Visual Basic IDE, shows us the &lt;A href="http://www.codeplex.com/clrinterop" target=_blank&gt;P/Invoke Interop Assistant available on CodePlex&lt;/A&gt;. The tool helps&amp;nbsp;with converting unmanaged C code to managed P/Invoke signatures and vice versa. Say goodbye to digging through random header files or MSDN documentation to find the right constants, structures and signatures. The P/Invoke Interop Assistant does a smarter translation for you using SAL (Source Code Annotation Language).&amp;nbsp;&lt;BR&gt;&lt;BR&gt;Enjoy,&lt;BR&gt;-&lt;A class="" href="http://blogs.msdn.com/bethmassi" target=_blank mce_href="http://blogs.msdn.com/bethmassi"&gt;Beth Massi&lt;/A&gt;, VS Community&lt;/SPAN&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8785968" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/Beth+Massi/default.aspx">Beth Massi</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/PInvoke/default.aspx">PInvoke</category></item><item><title>PInvoke Interop Assistant on CodePlex (Jared Parsons)</title><link>http://blogs.msdn.com/vbteam/archive/2008/06/20/pinvoke-interop-assistant-on-codeplex.aspx</link><pubDate>Fri, 20 Jun 2008 19:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8625741</guid><dc:creator>VBTeam</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/8625741.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=8625741</wfw:commentRss><description>&lt;P&gt;I'm happy to announce that the &lt;A href="http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx"&gt;PInvoke Interop Assistant&lt;/A&gt; tool is now available on CodePlex.&amp;nbsp; This includes the binaries, source code and the dev authored unit tests.&amp;nbsp; 
&lt;P&gt;It is hosted on the more general CLR Interop Tools page.&amp;nbsp; 
&lt;P&gt;&lt;A href="http://www.codeplex.com/clrinterop" mce_href="http://www.codeplex.com/clrinterop"&gt;http://www.codeplex.com/clrinterop&lt;/A&gt; 
&lt;P&gt;I will be actively maintaining this tool in the future and (hopefully) adding more features.&amp;nbsp; There were several features we cut just before release due to QA costing that I would like to add back (including VS integration, wrapper functions).&amp;nbsp;&amp;nbsp; 
&lt;P&gt;Please post any suggestions you have for the tool on the site and we will definitely consider them. &lt;/P&gt;
&lt;P&gt;Jared Parsons&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/jaredpar" mce_href="http://blogs.msdn.com/jaredpar"&gt;http://blogs.msdn.com/jaredpar&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8625741" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/VB6_5F00_Migration_2F00_Interop/default.aspx">VB6_Migration/Interop</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Did+you+know_3F00_/default.aspx">Did you know?</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/PInvoke/default.aspx">PInvoke</category></item><item><title>Making PInvoke Easy</title><link>http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx</link><pubDate>Fri, 14 Mar 2008 19:56:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8206410</guid><dc:creator>VBTeam</dc:creator><slash:comments>12</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/8206410.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=8206410</wfw:commentRss><description>&lt;p&gt;Jared here again.&amp;nbsp; &lt;/p&gt; &lt;p&gt;I very excited to announce we recently released a tool I've been working on to MSDN that will greatly help with using PInvoke in managed code.&amp;nbsp; The tool is called the "PInvoke Interop Assistant" and is included as part of a MSDN article on marshalling data for PInvoke and Reverse PInvoke scenarios.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Here is a link to the article and tool&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Article: &lt;a title="http://msdn2.microsoft.com/en-us/magazine/cc164193.aspx" href="http://msdn2.microsoft.com/en-us/magazine/cc164193.aspx"&gt;http://msdn2.microsoft.com/en-us/magazine/cc164193.aspx&lt;/a&gt;  &lt;li&gt;Tool: &lt;a title="CLRInsideOut2008_01.exe" href="http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/CLRInsideOut2008_01.exe"&gt;CLRInsideOut2008_01.exe&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The motivation behind this tool is writing PInvoke is a hard and often tedious task. There are many rules you must obey and many exceptions that must be taken into account.&amp;nbsp; Anything beyond simple data structures gets very involved and subtle semantics of C can greatly change the needed signature.&amp;nbsp; Incorrect translations often result in obscure exceptions or crashes.&lt;/p&gt; &lt;p&gt;In short, it's not any fun. &lt;/p&gt; &lt;p&gt;The tool works in several different ways to make PInvoke generation an easier process.&amp;nbsp; The goal is to make generating managed code for structs, unions, enums, constants, functions, typedefs , etc ... as easy as possible. The resulting code can be generated in both VB and C#. &lt;/p&gt; &lt;p&gt;The GUI version of the tool operates in 3 modes.&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;ol&gt; &lt;li&gt;SigImp Search: Search for a commonly used function and translate it into managed code.&lt;/li&gt; &lt;li&gt;SigImp Translate Snippet: Directly translate C code into managed PInvoke signatures.&lt;/li&gt; &lt;li&gt;SigExp: Convert managed binaries into C++ Reverse PInvoke scenarios&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;The first two are the parts I worked on and represent the PInvoke scenarios.&amp;nbsp; The third part was written by Ladi Prosek and will be covered in a different article. We chose the names SigImp and SigExp to mirror the tblimp/tlbexp tool base since they have similar functions.&lt;/p&gt; &lt;h3&gt;Directly translating C code into PInvoke Signatures&lt;/h3&gt; &lt;p&gt;Most adventures in PInvoke start with a developer having a small set of C code they would like to use from a managed binary.&amp;nbsp; Typically it's one or two functions with several supporting C structs.&amp;nbsp; Before, all of this would be hand translated into managed code from scratch.&amp;nbsp; With this tool all you must do is paste the code into the tool and it will generate the interop signature for you.&amp;nbsp; &lt;/p&gt; &lt;p&gt;For instance assume you wanted to translate the following C code into VB.&amp;nbsp; &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;struct&lt;/span&gt; S1
{
  &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; a;
  &lt;span style="color: rgb(0,0,255)"&gt;char&lt;/span&gt;[10] b;
};

&lt;span style="color: rgb(0,0,255)"&gt;float&lt;/span&gt; CalculateData(S1* p);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Start up the tool and switch to the "SigImp Translate Snippet" tab.&amp;nbsp; Then paste the code in and then hit the Generate button.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/MakingPInvokeEasy_E069/PInvoke1_4.png" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="157" alt="PInvoke1" src="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/MakingPInvokeEasy_E069/PInvoke1_thumb_1.png" width="244" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can also set click the "Auto Generate" box and watch the code update as you type.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;This translation is not limited to built-in C types.&amp;nbsp; It will also resolve most commonly used windows types such as HANDLE, DWORD all the way up to complex structs such as &lt;a href="http://msdn2.microsoft.com/en-us/library/aa365740(VS.85).aspx"&gt;WIN32_FIND_DATA&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;Searching for a commonly used function&lt;/h3&gt;
&lt;p&gt;Often developers want to use C functions familiar to them in managed code.&amp;nbsp; This can be a tedious task as well because if the signature is not already available you are back to coding from scratch.&amp;nbsp; Even adding a constant value can be tricky if you don't know which header file to look in.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The tool also provides a database of many commonly used functions, structs, constants, etc ... It is essentially anything that is included from windows.h.&amp;nbsp; Switch to the SigImp search tab, type the name of what you are looking for and hit generate.&amp;nbsp; For example if I want to see the value for WM_PAINT just type it in.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/MakingPInvokeEasy_E069/Pinvoke2_2.png" target="_blank"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="157" alt="Pinvoke2" src="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/MakingPInvokeEasy_E069/Pinvoke2_thumb.png" width="244" border="0"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;In addition this part of the tool will also do dependency calculation.&amp;nbsp; For instance if choose a method which has a parameter that is a C structure it will automatically generate the structure with the function.&amp;nbsp; For instance if you choose the function &lt;a href="http://msdn2.microsoft.com/en-us/library/aa364418.aspx"&gt;FindFirstFile&lt;/a&gt; it will determine that the function depends on the WIN32_FIND_DATA structure.&amp;nbsp; Furthermore it will notice that WIN32_FIND_DATA depends on FILETIME and generate both in addition to the method.&lt;/p&gt;&lt;pre class="code"&gt;&amp;lt;System.Runtime.InteropServices.StructLayoutAttribute( _
    System.Runtime.InteropServices.LayoutKind.Sequential, _
    CharSet:=System.Runtime.InteropServices.CharSet.[Unicode])&amp;gt; _
&lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Structure&lt;/span&gt; WIN32_FIND_DATAW
    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwFileAttributes &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''FILETIME-&amp;gt;_FILETIME
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; ftCreationTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; FILETIME
    &lt;span style="color: rgb(0,128,0)"&gt;'''FILETIME-&amp;gt;_FILETIME
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; ftLastAccessTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; FILETIME
    &lt;span style="color: rgb(0,128,0)"&gt;'''FILETIME-&amp;gt;_FILETIME
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; ftLastWriteTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; FILETIME
    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; nFileSizeHigh &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; nFileSizeLow &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwReserved0 &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwReserved1 &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''WCHAR[260]
&lt;/span&gt;    &amp;lt;System.Runtime.InteropServices.MarshalAsAttribute( _
        System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=260)&amp;gt; _
    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; cFileName &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;String
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''WCHAR[14]
&lt;/span&gt;    &amp;lt;System.Runtime.InteropServices.MarshalAsAttribute( _
        System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=14)&amp;gt; _
    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; cAlternateFileName &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;String
End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Structure

&lt;/span&gt;&amp;lt;System.Runtime.InteropServices.StructLayoutAttribute( _
    System.Runtime.InteropServices.LayoutKind.Sequential)&amp;gt; _
&lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Structure&lt;/span&gt; FILETIME
    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwLowDateTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwHighDateTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Structure

Partial&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Class&lt;/span&gt; NativeMethods
    &lt;span style="color: rgb(0,128,0)"&gt;'''Return Type: HANDLE-&amp;gt;void*
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''lpFileName: LPCWSTR-&amp;gt;WCHAR*
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''lpFindFileData: LPWIN32_FIND_DATAW-&amp;gt;_WIN32_FIND_DATAW*
&lt;/span&gt;    &amp;lt;System.Runtime.InteropServices.DllImportAttribute(&lt;span style="color: rgb(163,21,21)"&gt;"kernel32.dll"&lt;/span&gt;, EntryPoint:=&lt;span style="color: rgb(163,21,21)"&gt;"FindFirstFileW"&lt;/span&gt;)&amp;gt; _
    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Shared&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Function&lt;/span&gt; FindFirstFileW( _
        &amp;lt;System.Runtime.InteropServices.InAttribute(), _
            System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)&amp;gt; _
            &lt;span style="color: rgb(0,0,255)"&gt;ByVal&lt;/span&gt; lpFileName &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;String&lt;/span&gt;, _
        &amp;lt;System.Runtime.InteropServices.OutAttribute()&amp;gt; _
        &lt;span style="color: rgb(0,0,255)"&gt;ByRef&lt;/span&gt; lpFindFileData &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; WIN32_FIND_DATAW) &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; System.IntPtr
    &lt;span style="color: rgb(0,0,255)"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Function
End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Class&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;h3&gt;Translating Large Code bases&lt;/h3&gt;
&lt;p&gt;The snippet translator works well for small snippets of code.&amp;nbsp; If you are trying to translate a much larger code base, say several interdependent header files the small snippet dialog won't work well.&amp;nbsp; To work with larger code bases you should use the command line version of the tool;&amp;nbsp; sigimp.exe.&amp;nbsp; It is designed to process several header files and produce a mass output.&amp;nbsp; &lt;/p&gt;
&lt;h3&gt;Wrapping Up&lt;/h3&gt;
&lt;p&gt;This tool started out as a pet project of mine some time ago.&amp;nbsp; I'm extremely excited that customers are now going to be able to take advantage of it and I greatly look forward to any feedback you have.&amp;nbsp; I will post a couple more articles in the future detailing how this tool works under the hood.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Jared Parsons&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/jaredpar"&gt;http://blogs.msdn.com/jaredpar&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8206410" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/VB6_5F00_Migration_2F00_Interop/default.aspx">VB6_Migration/Interop</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Did+you+know_3F00_/default.aspx">Did you know?</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/PInvoke/default.aspx">PInvoke</category></item><item><title>Customizing Displays in the Debugger for System Types</title><link>http://blogs.msdn.com/vbteam/archive/2007/09/28/customizing-displays-in-the-debugger-for-system-types.aspx</link><pubDate>Sat, 29 Sep 2007 02:04:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5193108</guid><dc:creator>VBTeam</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/5193108.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=5193108</wfw:commentRss><description>&lt;p&gt;We've heard feedback from several customers regarding the way certain types are displayed in the Debugger.&amp;nbsp; Many of the displays exist to maintain the user experience between versions of Visual Studio.&amp;nbsp; We constantly evaluate if this is the correct choice for a given version of the product.&amp;nbsp; &lt;p&gt;Starting with VS2008, you don't have to wait for us any longer.&amp;nbsp; In VS2008, VB added full support for many of the debugging features it lacked compared to C# in 2005.&amp;nbsp; In particular we've added full support for the &lt;a href="http://msdn2.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx"&gt;DebuggerDisplayAttribute&lt;/a&gt;.&amp;nbsp; &lt;p&gt;By attributing a class or member with this attribute you can control how it is displayed in the debugger.&amp;nbsp; For each column (name, value and type) you can provide an alternate string or expression to display.&amp;nbsp; &lt;p&gt;The best part about this attribute is you can target types that exist in different libraries.&amp;nbsp; You don't even need the source for them.&amp;nbsp; One of the members in the Type field which species the target type.&amp;nbsp; Customizing a type in a separate library requires slightly more work than customizing a type you have the source for.&amp;nbsp; For a source project you can just apply the attribute directly to the type or member and it will display.&amp;nbsp; For a type in another library you need to do the following.  &lt;ul&gt; &lt;li&gt;Define a class library and include all of the &lt;a href="http://msdn2.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx"&gt;DebuggerDisplayAttribute&lt;/a&gt; you want.&amp;nbsp; Make sure to apply the attributes to the assembly and specify the &lt;a href="http://msdn2.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.type.aspx"&gt;Type&lt;/a&gt; member.&amp;nbsp; Ex.&lt;/li&gt;&lt;/ul&gt; &lt;blockquote&gt;&lt;pre&gt;&amp;lt;Assembly: DebuggerDisplay("{ToString}", Target:=GetType(Guid))&amp;gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Place the built library under the folder "Visual Studio 2008\Visualizers" which is under your my documents folder.&amp;nbsp; &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;After doing this any Guid type will now show up as the actual Guid String ("10f3c4eb-7c0f-41b1-ae83-8838ff2f4f70") instead of {System.Guid} 
&lt;p&gt;Jared &lt;a title="http://blogs.msdn.com/jaredpar/" href="http://blogs.msdn.com/jaredpar/"&gt;http://blogs.msdn.com/jaredpar/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5193108" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/IDE/default.aspx">IDE</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/VB2008/default.aspx">VB2008</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Debugger/default.aspx">Debugger</category></item><item><title>Closures in VB Part 5: Looping</title><link>http://blogs.msdn.com/vbteam/archive/2007/07/26/closures-in-vb-part-5-looping.aspx</link><pubDate>Thu, 26 Jul 2007 19:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4068148</guid><dc:creator>VBTeam</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/4068148.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=4068148</wfw:commentRss><description>&lt;P&gt;For previous articles in the series please see&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/05/02/closures-in-vb-part-1.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/05/02/closures-in-vb-part-1.aspx"&gt;Part 1: Introduction&lt;/A&gt; 
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx"&gt;Part 2: Method Calls&lt;/A&gt; 
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/05/25/closures-in-vb-part-3-scope.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/05/25/closures-in-vb-part-3-scope.aspx"&gt;Part 3: Scope&lt;/A&gt; 
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/06/15/closures-in-vb-part-4-variable-lifetime.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/06/15/closures-in-vb-part-4-variable-lifetime.aspx"&gt;Part 4: Variable Lifetime&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Once again sorry for the long delay between posts.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looping structures can cause unintended consequences when used with Lambda expressions.&amp;nbsp; The problem occurs because lambda expressions do not execute when they are constructed but rather when they are invoked.&amp;nbsp; For example take the following code.&amp;nbsp; &lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; LoopExample1()
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; list &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; List(Of Func(Of &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;))
        &lt;SPAN style="COLOR: blue"&gt;For&lt;/SPAN&gt; i = &lt;SPAN style="COLOR: maroon"&gt;0&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;To&lt;/SPAN&gt; &lt;SPAN style="COLOR: maroon"&gt;3&lt;/SPAN&gt;
            list.Add(&lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;() i)
        &lt;SPAN style="COLOR: blue"&gt;Next&lt;/SPAN&gt;

        &lt;SPAN style="COLOR: blue"&gt;For&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Each&lt;/SPAN&gt; cur &lt;SPAN style="COLOR: blue"&gt;In&lt;/SPAN&gt; list
            Console.Write(&lt;SPAN style="COLOR: maroon"&gt;"{0} "&lt;/SPAN&gt;, cur())
        &lt;SPAN style="COLOR: blue"&gt;Next&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Many users are surprised to find out the above will print "4 4 4 4 ".&amp;nbsp; The reason goes back to my previous 2 posts on variable lifetime and scope.&amp;nbsp; All "For" and "For Each" blocks in Vb have 2 scopes.&amp;nbsp; &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Scope where iteration variables are defined 
&lt;LI&gt;Body of the for loop&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;The first scope is entered only once no matter how many times the loop is executed.&amp;nbsp; The second is entered once per iteration of the loop.&amp;nbsp; Any iteration variables that are defined in a For/For Each loop are created in the first scope (in this case "i" and "cur").&amp;nbsp; Hence there is only one of those variables for every loop iteration and the lambda function lifts the single variable "i".&amp;nbsp; &lt;/P&gt;
&lt;P&gt;This has thrown off many users because the behavior works most of the time.&amp;nbsp; For instance if I switched the code to run "Console.Write" inside the first loop, it would print out&amp;nbsp;"0 1 2 3 "&amp;nbsp;as expected.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;To mitigate against this problem the above code will actually produce a warning in VB.&amp;nbsp; &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;warning BC42324: Using the iteration variable in a lambda expression may have unexpected results.&amp;nbsp; Instead, create a local variable within the loop and assign it the value of the iteration variable.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There are two ways to fix this problem depending on the behavior you want.&amp;nbsp; If you see this warning and don't know if it affects you, the safest change is to do the following.&amp;nbsp; &lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; LoopExample2()
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; list &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; List(Of Func(Of &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;))
        &lt;SPAN style="COLOR: blue"&gt;For&lt;/SPAN&gt; iTemp = &lt;SPAN style="COLOR: maroon"&gt;0&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;To&lt;/SPAN&gt; &lt;SPAN style="COLOR: maroon"&gt;3&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; i = iTemp
            list.Add(&lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;() i)
        &lt;SPAN style="COLOR: blue"&gt;Next&lt;/SPAN&gt;

        &lt;SPAN style="COLOR: blue"&gt;For&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Each&lt;/SPAN&gt; cur &lt;SPAN style="COLOR: blue"&gt;In&lt;/SPAN&gt; list
            Console.Write(&lt;SPAN style="COLOR: maroon"&gt;"{0} "&lt;/SPAN&gt;, cur())
        &lt;SPAN style="COLOR: blue"&gt;Next&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;This will cause "i" to be created in the second scope.&amp;nbsp; Hence there will be a different value for every loop iteration and the code will print out&amp;nbsp;"0 1 2 3"&amp;nbsp;as expected.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;If you do want the code to print out "4 4 4 4 " then add "Dim i = 0" before the start of the loop.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Jared Parsons(&lt;A href="http://blogs.msdn.com/jaredpar" mce_href="http://blogs.msdn.com/jaredpar"&gt;http://blogs.msdn.com/jaredpar&lt;/A&gt;)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4068148" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category></item><item><title>Closures in VB Part 4: Variable Lifetime</title><link>http://blogs.msdn.com/vbteam/archive/2007/06/15/closures-in-vb-part-4-variable-lifetime.aspx</link><pubDate>Sat, 16 Jun 2007 00:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3320082</guid><dc:creator>VBTeam</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/3320082.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=3320082</wfw:commentRss><description>&lt;P&gt;For previous articles in this series please see 
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/05/02/closures-in-vb-part-1.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/05/02/closures-in-vb-part-1.aspx"&gt;Part 1: Introduction&lt;/A&gt; 
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx"&gt;Part 2: Method Calls&lt;/A&gt; 
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/05/25/closures-in-vb-part-3-scope.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/05/25/closures-in-vb-part-3-scope.aspx"&gt;Part 3: Scope&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Sorry for the long delay between posts here.&amp;nbsp; We're getting Orcas out the door and getting this series completed takes a back seat to shipping.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Originally I wanted to talk about looping structures next.&amp;nbsp; However when I started writing that post I realized that I had to talk about lifetime before the looping structures would make sense.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Prior to Orcas the lifetime of a variable in VB was the entire function.&amp;nbsp; This presented several problems from a closures perspective.&amp;nbsp; Imagine you had a looping structure and the value was used in a lambda expression.&amp;nbsp; &lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; LifetimeExample()
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; list &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; List(Of Func(Of &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;))
        &lt;SPAN style="COLOR: blue"&gt;For&lt;/SPAN&gt; i = &lt;SPAN style="COLOR: maroon"&gt;0&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;To&lt;/SPAN&gt; &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;

            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; x = i * &lt;SPAN style="COLOR: maroon"&gt;2&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; &lt;SPAN style="COLOR: maroon"&gt;True&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Then&lt;/SPAN&gt;
                list.Add(&lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;() x)
            &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Next&lt;/SPAN&gt;

        &lt;SPAN style="COLOR: blue"&gt;For&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Each&lt;/SPAN&gt; f &lt;SPAN style="COLOR: blue"&gt;In&lt;/SPAN&gt; list
            Console.Write(f() &amp;amp; " ")
        &lt;SPAN style="COLOR: blue"&gt;Next&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;In this example if we left the lifetime rules unchanged, there would be a single variable "x" for the entire function.&amp;nbsp; That means that we would end up printing out &lt;/P&gt;
&lt;P&gt;10 10 10 10 10&lt;/P&gt;
&lt;P&gt;This is somewhat unexpected and essentially means that VB could not support complex Lambda scenarios.&amp;nbsp; To fix this we altered the lifetime of variables to be tied to the scope they were contained in.&amp;nbsp; The end effect is that each iteration of the loop has a separate "x" since each iteration enters and leaves the scope of the "if" statement.&amp;nbsp; As a result it will print out &lt;/P&gt;
&lt;P&gt;0 2 4 6 8 10&lt;/P&gt;
&lt;P&gt;We did make one backcompat adjustment for this change.&amp;nbsp; The lifetime of variables in VB was visible if you tried to use an uninitialized variable in a loop/goto.&amp;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 "x" has a lifetime longer than the loop.&lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; VisibleLifetime()
        &lt;SPAN style="COLOR: blue"&gt;For&lt;/SPAN&gt; i = &lt;SPAN style="COLOR: maroon"&gt;0&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;To&lt;/SPAN&gt; &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; x &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
            Console.WriteLine(x)
            x += &lt;SPAN style="COLOR: maroon"&gt;2&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Next&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;To make sure that we didn't break any existing code we had one little errata for the change.&amp;nbsp; When a variable'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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jared Parsons (&lt;A href="http://blogs.msdn.com/jaredpar" mce_href="http://blogs.msdn.com/jaredpar"&gt;http://blogs.msdn.com/jaredpar&lt;/A&gt;&amp;nbsp;)&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3320082" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category></item><item><title>Closures in VB Part 3: Scope</title><link>http://blogs.msdn.com/vbteam/archive/2007/05/25/closures-in-vb-part-3-scope.aspx</link><pubDate>Sat, 26 May 2007 00:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2880645</guid><dc:creator>VBTeam</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/2880645.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=2880645</wfw:commentRss><description>&lt;P&gt;Jared here again.&amp;nbsp; For previous articles in this series please see&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/05/02/closures-in-vb-part-1.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/05/02/closures-in-vb-part-1.aspx"&gt;Part 1: Introduction&lt;/A&gt; 
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx"&gt;Part 2: Method Calls&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Thus far in the series we've only lifted variables that are declared in the same block/scope.&amp;nbsp;What happens if we lift variables in different scope?&amp;nbsp; The answer is that one closure class will be created for every unique scope where a lifted variable is declared and all of the variables in that scope that are lifted will be placed in that closure.&amp;nbsp; Once again, examples speak best&lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; Scope1()
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; x = &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f1 = &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; z &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;) x + z
        Console.WriteLine(f1(&lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;))
        &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; x &amp;gt; &lt;SPAN style="COLOR: maroon"&gt;2&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Then&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; y = &lt;SPAN style="COLOR: maroon"&gt;6&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; g = &lt;SPAN style="COLOR: maroon"&gt;7&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f2 = &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; z &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;) z + y + g
            Console.WriteLine(f2(&lt;SPAN style="COLOR: maroon"&gt;4&lt;/SPAN&gt;))
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;The code will end up looking like so ...&lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; Closure1
        &lt;SPAN style="COLOR: blue"&gt;Public&lt;/SPAN&gt; x &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;

        &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; Lambda_f1(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; z &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;)
            &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; x + z
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; Closure2
        &lt;SPAN style="COLOR: blue"&gt;Public&lt;/SPAN&gt; y &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Public&lt;/SPAN&gt; g &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;

        &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; Lambda_f2(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; z &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;)
            &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; y + z + g
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; Scope1()
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; c1 &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; Closure1()
        c1.x = &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
        Console.WriteLine(c1.Lambda_f1(&lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;))
        &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; c1.x &amp;gt; &lt;SPAN style="COLOR: maroon"&gt;2&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Then&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; c2 &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; Closure2()
            c2.y = &lt;SPAN style="COLOR: maroon"&gt;6&lt;/SPAN&gt;
            c2.g = &lt;SPAN style="COLOR: maroon"&gt;7&lt;/SPAN&gt;
            Console.WriteLine(c2.Lambda_f2(&lt;SPAN style="COLOR: maroon"&gt;4&lt;/SPAN&gt;))
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;There are a couple of items to take away from this example.&amp;nbsp; &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Only two closure classes were created even though three variables were lifted.&amp;nbsp; The number of closures only depends on the number of scopes of all of the lifted declared variables. 
&lt;LI&gt;The closures are created at the begining of the scope they are associated and not at the begining of the method.&amp;nbsp; This will be more important in the next part of the series. 
&lt;LI&gt;Each lambda instance is attached to the closure associated with the scope the lambda is declared in.&amp;nbsp; &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;The next&amp;nbsp;twist is what were to happen if&amp;nbsp;the lambda "f2" were to also use the variable "x".&amp;nbsp; As it's currently written there is no association between Closure1 and Closure2 therefore&amp;nbsp;there is no way for it to access the lifted variable.&amp;nbsp; The answer is two fold.&amp;nbsp;&amp;nbsp;Firstly to&amp;nbsp;reduce clutter I pasted the closure classes as if they were separate entries.&amp;nbsp; In fact Closure2 would appear as a nested class of Closure1 in the real generated code.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Secondly if x were used inside of "f2", the real use would be "c1.x".&amp;nbsp; That's (almost) no different than "someOtherVar.x".&amp;nbsp; Therefore the instance of c1 will be lifted into Closure2.&amp;nbsp; &lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f2 = &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; z &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;) z + y + g + x&lt;/PRE&gt;
&lt;P&gt;Woud result in the following definition of Closure2 ...&lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; Closure2
        &lt;SPAN style="COLOR: blue"&gt;Public&lt;/SPAN&gt; y &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Public&lt;/SPAN&gt; g &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Public&lt;/SPAN&gt; c1 &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; Closure1

        &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; Lambda_f2(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; z &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;)
            &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; y + z + g + c1.x
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;In deeply nested lambdas and scopes this type of lifting will continue recursively.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;That's it for this entry, the next article will talk about looping structures and possibly variable lifetime.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2880645" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/LINQ_2F00_VB9/default.aspx">LINQ/VB9</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category></item><item><title>Closures in VB Part 2: Method Calls</title><link>http://blogs.msdn.com/vbteam/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx</link><pubDate>Fri, 04 May 2007 02:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2401443</guid><dc:creator>VBTeam</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/2401443.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=2401443</wfw:commentRss><description>&lt;P&gt;For previous articles in this series, please see&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/vbteam/archive/2007/05/02/closures-in-vb-part-1.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2007/05/02/closures-in-vb-part-1.aspx"&gt;Part 1 - The basics&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Jared here again.&amp;nbsp; This part of the series will focus on how method calls are handled in closures.&amp;nbsp;&amp;nbsp;As stated in the previous article, the purpose of closures is to allow all operations inside a lambda or query expression that would normally be available inside the function or sub.&amp;nbsp; To do this closures often need to capture (or lift) relevant variables from the function into the generated class.&lt;/P&gt;
&lt;P&gt;There are&amp;nbsp;2 types of methods and method calls that closures have to handle.&amp;nbsp; &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Method calls to a shared method or methods on modules. 
&lt;LI&gt;Method calls to instance members of a class &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;STRONG&gt;Scenario #1 &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Below is an example of a method call inside a lambda expression for scenario #1.&amp;nbsp; &lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: blue"&gt;Module&lt;/SPAN&gt; M1

    &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; MyValue() &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; &lt;SPAN style="COLOR: maroon"&gt;42&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; Example1()
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; x = &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f = &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;() x + MyValue()
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;

&lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Module&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Here we are calling a&amp;nbsp;module method inside a lambda.&amp;nbsp; Module Methods or Shared methods can be called from anywhere because they require no specific variable for the call.&amp;nbsp; This requires no special work from closures as the call can just be made naturally.&lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; Closure
        &lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; x &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;

        &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; Lambda_f() &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; x + M1.MyValue
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;
&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Scenario #2&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Calling an instance method is more difficult than a shared method because it requires the referenc "Me".&amp;nbsp; If you don't type this specifically in code the VB Compiler will add it for you under the hood.&amp;nbsp; To make this work the closures code will also "lift" the variable "Me" in the same way that it lifts normal variables in a function.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Calling a instance method inside a lambda expression is little difference than calling a member method on a variable used in a lambda.&amp;nbsp; The only difference is the variable is "Me".&amp;nbsp; For example&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; C1
    &lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; m_myValue &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; MyValue() &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; m_myValue
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; Example2()
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; x = &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f = &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;() x + MyValue()
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;
&lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;In this case we need to access both "x" and "Me.MyValue()" from the closure.&amp;nbsp; The generated code will create space for both of these variables and the transformed code in Example2 will store both of the values.&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; Closure
    &lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; x &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; OriginalMe &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; C1

    &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; Lambda_f()
        &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; x + OriginalMe.MyValue()
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;
&lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; Example2()
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; c &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; Closure
        c.x = &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
        c.OriginalMe = &lt;SPAN style="COLOR: blue"&gt;Me&lt;/SPAN&gt;
        &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f = &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; Func(Of &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;)(&lt;SPAN style="COLOR: blue"&gt;AddressOf&lt;/SPAN&gt; c.Lambda_f)
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;As usual, the generated code is much uglier but this essentially what will be generated.&amp;nbsp; That wraps it up for method calls.&amp;nbsp; In the next part, I will discuss the variable liftetime and scoping issues that come into play with closures.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Jared Parsons&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/jaredpar" mce_href="http://blogs.msdn.com/jaredpar"&gt;http://blogs.msdn.com/jaredpar&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2401443" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/LINQ_2F00_VB9/default.aspx">LINQ/VB9</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category></item><item><title>Closures in VB: Part 1</title><link>http://blogs.msdn.com/vbteam/archive/2007/05/02/closures-in-vb-part-1.aspx</link><pubDate>Thu, 03 May 2007 06:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2386096</guid><dc:creator>VBTeam</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/2386096.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=2386096</wfw:commentRss><description>&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My name is Jared Parsons and I am a developer on the VB Compiler and Debugger.&amp;nbsp; One of the features I implemented for VB 9.0 is lexical closure support.&amp;nbsp; This a great addition to the VB language and I wanted to do a series of blog posts to describe this feature and how it will impact your code.&lt;/P&gt;
&lt;P&gt;Lexical Closures (more often referred to as simply Closures) are the underpinnings for several new features in Visual Basic 9.0.&amp;nbsp; The are part of the guts of Lambda and Query expressions.&amp;nbsp; This will be a several part series on Closures in VB 9.0; how they work, their limitations, pitfalls surrounding their use.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;To start off, let's get a basic summary of what a Closure is.&amp;nbsp; &lt;A href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29" target=_blank mce_href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29"&gt;Wikipedia&lt;/A&gt; defines it as "... a&amp;nbsp; is a semantic concept referring to a function paired with an environment ...".&amp;nbsp; I prefer to describe it as follows.&amp;nbsp; A closure is a feature which allows users to seemlessly access an environment (locals, parameters&amp;nbsp;and methods) from more than one function.&amp;nbsp; Even better are samples :) &lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; C1
        &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; Test()
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; x = &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f = &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; y &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;) x + y
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; result = f(&lt;SPAN style="COLOR: maroon"&gt;42&lt;/SPAN&gt;)
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;In this code we have a lambda expression which takes in a single parameter and adds it with a local variable.&amp;nbsp; Lambda expressions are implemented as functions in VB (and C#).&amp;nbsp; So now we have two functions, "Test" and "f", which are accessing a single local variable.&amp;nbsp; This is where closures come into play.&amp;nbsp; Closures are responsible for making the single variable "x" available to both functions in a process that is referred to as "lifting the variable".&lt;/P&gt;
&lt;P&gt;To do this the compiler will take essentially&amp;nbsp;4 actions.&amp;nbsp; &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a class which will contain "x" in order to share it among both functions.&amp;nbsp; Call it "Closure" for now 
&lt;LI&gt;It will create a new function for the lambda expression in the class "Closure".&amp;nbsp; Call it "f" for now 
&lt;LI&gt;Create a new instance of the class "Closure" inside the sub "Test" 
&lt;LI&gt;Rewrite all access of "x" into the member "x" of "Closure".&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; Closure
        &lt;SPAN style="COLOR: blue"&gt;Public&lt;/SPAN&gt; x &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;

        &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; f(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; y &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;) &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; x + y
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; C1
        &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; Test()
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; c &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; Closure()
            c.x = &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; Func(Of &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;) = &lt;SPAN style="COLOR: blue"&gt;AddressOf&lt;/SPAN&gt; c.f
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; result = f(&lt;SPAN style="COLOR: maroon"&gt;42&lt;/SPAN&gt;)
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Now "x" is shared amongst both functions and the user didn't have to know anything about the code we generated.&amp;nbsp; You can see from this simplified example just how much code Closures and all of the other new VB 9.0 features are saving you here (Type Inference, Lambda Expressions).&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Note this is only a simulation of what is generated when you use a closure, the actual generated code is much uglier and involves lots of unbindable names "$Lambda_1", etc ... &lt;/P&gt;
&lt;P&gt;In the next part of this article I'll dive into some more uses of closures (multiple variables, method access, &amp;nbsp;terminology, etc...).&lt;/P&gt;
&lt;P&gt;Jared Parsons&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.msdn.com/jaredpar" mce_href="https://blogs.msdn.com/jaredpar"&gt;https://blogs.msdn.com/jaredpar&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2386096" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/LINQ_2F00_VB9/default.aspx">LINQ/VB9</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category></item></channel></rss>