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 reference 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.