Public Variables In VB.NET Modules

Interesting bit of detective work the other day I thought I'd share with everyone. A friend of a friend was having trouble with an ASP 2.0 project that was ported to ASP.NET. Turns out that on some web servers the different clients were getting their data overwritten by others. A little splunking uncovered that the application was maintaining state using a public variable declared in a VB module. I could guess what was happening, but wanted to know the details so I created a tiny test app that looked like this:

Module Module1
    Public moduleinteger As Integer
End Module

I compiled and then decompiled the assembly into C# with the following results:

namespace ModuleTest
{
        internal sealed class Module1
        {
                public static int moduleinteger;
        } 
}

So essentially the vb compiler turns the module keyword into the c# equivalent "internal sealed class" and makes all public variable static. Also any refernece to the public module variable gets the "module" name added to it as well. And since static variables are the same across all threads any update to one, updates all.

8 Comments

  • This is actually the same behavior VB6 modules had, but apparently most VB6 developers never realized this since they worked primarily with Windows apps that serviced only one user at a time. I actually had a hard time tracking down an error in VB6 that resulted from this one time that a predecessor had did. Anyhow, this comes up often in the various forums, so its a rather significant stumbling block in ASP.NET for VB developers.

  • Well, I have come across this most disturbing phenomena and must say it is driving me insane. I am trying to create a set of "globally" accessible variables from within a page, and any user controls it contains without having to pass the variables back and forth betweent the calling page and the user controls each time.

    Any Ideas as to how to make this happen, as using the public keyword seems to automatically make them static no matter what I say and screws up my pages between requests.

  • thanks
    i was facing the problem of declaring public variables and their scope.. this help provide me to customize my apps.

    chandan bhakuni

  • I don't beleive this is exactly the same problem as in VB6 (maybe Alex can convince me I am wrong). We had in VB6 this exact same problem only when we declared variables in a module Global the problem does not occur at our site when we use Public in VB6 modules.

  • hi

    i m also facing the same problem(public variable being accessed by all the threads) ...
    have you got any way to solve this problem.

    its driving me crazy

    Thanks in advance

    Sivabalan K

  • The easiest way might be to use session variables or thread local storage. It depends on what your code is trying to do.

  • Simply use the following way for storing and retrieving a value accross your ASP.NET pages:

    *****Storing a value*****

    session("myVariable") = "assign value here"

    *****Retrieving a value*****

    textbox1.text = session("myVariable")

    Remember: By Default, these session variables lose their values after 20 minutes since this is the default timeout in ASP.NET. To change this timeout length, simply modify your web.config file accordingly.

    Happy Coding!

  • Thank. I wanted to confirm my understanding of module variables. This post confirms that my understanding was perfect.

Comments have been disabled for this content.