-1

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?

variable
  • 8,262
  • 9
  • 95
  • 215

4 Answers4

2

you can create public static class

public static class GlobalVariable{
public static int globalvariable = 100;

}

then you can access

GlobalVariable.globalvariable
Gayan
  • 2,750
  • 5
  • 47
  • 88
1

Session and Application variables are "global" on Web platforms.

  • Session VAR (global to user session)
  • Application VAR (global to IIS process for website, I.e. all sessions accessing web site/app)

A Module, with a public variable, will be global to all forms/classes in a windows type app.

Louis van Tonder
  • 3,664
  • 3
  • 31
  • 62
1

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.

Ananth
  • 108
  • 6
0

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.

ThisHandleNotInUse
  • 1,135
  • 1
  • 10
  • 23