This is a really simple question and I’m suprised I have to ask it but…
How does one declare a readonly local variable in VB.Net?
Java and C++ have final/const local variables so I’m sure VB.Net must have them, but I just can’t find the syntax for it.
Unfortunately, VB.NET only supports readonly fields not readonly locals. VB.NET does not have anything like C++’s
constmodifier to mark a variable as readonly.Depending on the type of the variable, the
Constmodifier might do the job but it doesn’t mean the same thing as C++’sconst. In VB.NET,Constis simply a variable whose value is known at compilation time, thus allowing the compiler to replace all usages of that variable in the source code with the value itself.While the compiler will prevent you from modifying a
Constvariable you are severely limited in your options for the types that you can mark asConstsince most types cannot provide a known value at compilation time.