Closures in VB Part 4: Variable Lifetime

Published 15 June 07 02:04 PM

For previous articles in this series please see

Sorry for the long delay between posts here.  We're getting Orcas out the door and getting this series completed takes a back seat to shipping. 

Originally I wanted to talk about looping structures next.  However when I started writing that post I realized that I had to talk about lifetime before the looping structures would make sense. 

Prior to Orcas the lifetime of a variable in VB was the entire function.  This presented several problems from a closures perspective.  Imagine you had a looping structure and the value was used in a lambda expression. 

    Sub LifetimeExample()
        Dim list As New List(Of Func(Of Integer))
        For i = 0 To 5

            Dim x = i * 2
            If True Then
                list.Add(Function() x)
            End If
        Next

        For Each f In list
            Console.Write(f() & " ")
        Next
    End Sub

In this example if we left the lifetime rules unchanged, there would be a single variable "x" for the entire function.  That means that we would end up printing out

10 10 10 10 10

This is somewhat unexpected and essentially means that VB could not support complex Lambda scenarios.  To fix this we altered the lifetime of variables to be tied to the scope they were contained in.  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.  As a result it will print out

0 2 4 6 8 10

We did make one backcompat adjustment for this change.  The lifetime of variables in VB was visible if you tried to use an uninitialized variable in a loop/goto.  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.

    Sub VisibleLifetime()
        For i = 0 To 5
            Dim x As Integer
            Console.WriteLine(x)
            x += 2
        Next
    End Sub

To make sure that we didn't break any existing code we had one little errata for the change.  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. 

Jared Parsons (http://blogs.msdn.com/jaredpar ) 

by VBTeam
Filed under: ,

Comments

# Scott Hoggarth said on June 15, 2007 6:56 PM:

Thanks Jared!  You offer insight to both the future and the past.

# Bud Labitan said on June 22, 2007 10:29 AM:

I am pleased to announce the release of new open-source software in vb6 that provides simulated Warren Buffett responses in basic investing. Bud Labitan and Greg Binning have combined their open sourced vb code and investing workbook into a free software application called archbot. Archbot A.I. open-source software combined with xml brainfiles provides simulated Warren Buffett like responses to your typed questions on investing. All the files, including support and source code are in one 27 MB download here:

http://www.frips.com/setup1.exe  We welcome comments and volunteers who are motivated to enhance and make this free project even better.

If you wish to discuss this project, feel free to call me at 219-677-6281.

Best,

Bud Labitan

www.frips.com

# The Visual Basic Team said on July 26, 2007 12:42 PM:

For previous articles in the series please see Part 1: Introduction Part 2: Method Calls Part 3: Scope

# The Visual Studio Code Analysis Team Blog said on September 21, 2007 12:17 PM:

One of my favorite new features for Code Analysis in Visual Studio 2008 is our support for analyzing

New Comments to this post are disabled

This Blog

Syndication

Page view tracker