Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3354616
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:17:41+00:00 2026-05-18T02:17:41+00:00

Today I discovered that something I had assumed about VB.NET for many years was

  • 0

Today I discovered that something I had assumed about VB.NET for many years was not true (worrying!). I assumed that a variable declared within a loop had a lifetime of the iteration it was declared in, but in fact it seems it has a lifetime of the whole procedure.

For example:

        For i As Integer = 0 To 1
            Dim var1 As Boolean
            Console.WriteLine(var1.ToString())
            var1 = True
            Console.WriteLine(var1.ToString())
        Next
        Console.ReadKey()

I had assumed an output of False, True, False, True but instead it is actually False, True, True, True.

In C# the equivalent code would not compile as you would get a compile time error of Error “Use of unassigned local variable ‘var1′”.

I realise there are many ways to fix this and that best practice would be to declare the variable outside of the loop and reset it at the beginning of every loop through.

I find this behaviour so counter-intuitive to me that I would like at least a compile time warning in VB.NET when/if I do this. (I could also then set this on any projects I already have and get warning that would allow me to check that my assumptions aren’t causing errors).

Does anyone know how/if I can get this to generate a compile time warning in VB.NET? Am I the only one that finds this counter-intuitive?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:17:42+00:00Added an answer on May 18, 2026 at 2:17 am

    We’ll have to work on fixing your intuition because getting an error out of the compiler is not an option. It is partially implemented, you can get this warning:

    error BC42104: Variable ‘mumble’ is used before it has been assigned a value. A null reference exception could result at runtime.

    And elevate it from a warning to an error with Project + Properties, Compile tab. However, as the warning message indicates, this is only supported for reference type references, it won’t budge for a variable of a value type.

    Okay, intuition. If the runtime would implement your desired behavior then it would have to allocate a new variable for each iteration of the loop. Which implies that the number of local variables is bounded only by the number of iterations. This is very wasteful and a very easy trigger for StackOverflowException. The JIT compiler doesn’t do this, it re-uses the variable. This happens in C# as well, minus the option of letting you not initialize the value explicitly of course.

    Fwiw: I very much agree with you that this is unhelpful behavior. You’ll probably find receptive ears at connect.microsoft.com, post your feature request there and the VB.NET team will see it. There has been strong backing from customers as well as within MSFT to make VB.NET and C# feature comparable. If you post a link to your feedback report then I’ll be happy to vote it up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I discovered something that makes me sad: objects of type System.Generic.Collections.List don't have
Today I discovered that my fresh installation of Apache HTTP Server is able to
Today I was listening to the Hanselminutes show about .NET 3.5 SP1...What's inside ,
Today when I was in computer organization class, teacher talked about something interesting to
Today I had a discussion with a colleague about nested functions in Javascript: function
Today I had a coworker suggest I refactor my code to use a label
Today I stumbled about a Problem which seems to be a bug in the
Today, I ran into this weird problem with a user using Mac OS X.
Today I was working on a tab navigation for a webpage. I tried the
Today at work we came across the following code (some of you might recognize

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.