In VB6 we have global variables which are declared in module files.
In .NET, do we have such a concept? Or is it replaced by session variables and things like static constant variables of class which can be used as global data?
In VB6 we have global variables which are declared in module files.
In .NET, do we have such a concept? Or is it replaced by session variables and things like static constant variables of class which can be used as global data?
you can create public static class
public static class GlobalVariable{
public static int globalvariable = 100;
}
then you can access
GlobalVariable.globalvariable
Session and Application variables are "global" on Web platforms.
A Module, with a public variable, will be global to all forms/classes in a windows type app.
In VB.NET, you can add module file. In that module file, you had to declare variable and/or functions with Public. But this is only in VB.NET
Example, module file would be like this
Module UserDetails
Public SqlCon as SqlConnection
Public DataSet as DataSet
Public dataAdaptr as SqlDataAdapter
End Module
In above example, i am using sql connection, data set, data adapter from any form, class and module.
This example is being used in my projects already. You can use this in your Asp.net projects too.
I'll probably get in trouble for mentioning it as it's quite the taboo subject. There are also singleton classes which are class objects which can be instantiated but only one can exist at a time. I use one for a custom console for Winforms. But, yes, you can also use a public static class with static variables. These can be modified but you'll get in trouble with the "object oriented ethos" religious people due to it violating the sacred principles of encapsulation.
Honestly, I've only once needed what might otherwise be termed a "global variable" and that's just how much space I want to take up on my console singleton before it starts erasing old lines.
What do you need a global variable for? They're a pain to use with multithreading most of the time and they usually indicate you're not thinking about reusable code. Global variables are the duct tape of object oriented programming.