Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare global variables in Visual Studio 2010 and VB.NET

How do I declare a global variable in Visual Basic?

These variables need to be accessible from all the Visual Basic forms. I know how to declare a public variable for a specific form, but how do I do this for all the forms in my project?

like image 574
Tal Avatar asked Dec 25 '10 09:12

Tal


People also ask

How do you declare a global variable in VB net?

There is no way to declare global variables as you're probably imagining them in VB.NET. However, you'll need to fully-qualify all references to those variables anywhere you want to use them in your code.

Where do we declare a global level variable in Visual Basic?

VBA Global Variables are variables which are declared before the start of any macro in the module. When the variables are declared by using either “Public” or “Global,” it becomes “Global Variable.” Sub Procedure Variables Cannot Use Anywhere. We usually declare the variable inside the subroutine in VBA.

How are variables declared in VB net?

In VB.NET, const is a keyword that is used to declare a variable as constant. The Const statement can be used with module, structure, procedure, form, and class. Syntax: Const constname As datatype = value.


2 Answers

There is no way to declare global variables as you're probably imagining them in VB.NET.

What you can do (as some of the other answers have suggested) is declare everything that you want to treat as a global variable as static variables instead within one particular class:

Public Class GlobalVariables     Public Shared UserName As String = "Tim Johnson"     Public Shared UserAge As Integer = 39 End Class 

However, you'll need to fully-qualify all references to those variables anywhere you want to use them in your code. In this sense, they are not the type of global variables with which you may be familiar from other languages, because they are still associated with some particular class.

For example, if you want to display a message box in your form's code with the user's name, you'll have to do something like this:

Public Class Form1: Inherits Form      Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load         MessageBox.Show("Hello, " & GlobalVariables.UserName)     End Sub  End Class 

You can't simply access the variable by typing UserName outside of the class in which it is defined—you must also specify the name of the class in which it is defined.


If the practice of fully-qualifying your variables horrifies or upsets you for whatever reason, you can always import the class that contains your global variable declarations (here, GlobalVariables) at the top of each code file (or even at the project level, in the project's Properties window). Then, you could simply reference the variables by their name.

Imports GlobalVariables 

Note that this is exactly the same thing that the compiler is doing for you behind-the-scenes when you declare your global variables in a Module, rather than a Class. In VB.NET, which offers modules for backward-compatibility purposes with previous versions of VB, a Module is simply a sealed static class (or, in VB.NET terms, Shared NotInheritable Class). The IDE allows you to call members from modules without fully-qualifying or importing a reference to them. Even if you decide to go this route, it's worth understanding what is happening behind the scenes in an object-oriented language like VB.NET. I think that as a programmer, it's important to understand what's going on and what exactly your tools are doing for you, even if you decide to use them. And for what it's worth, I do not recommend this as a "best practice" because I feel that it tends towards obscurity and clean object-oriented code/design. It's much more likely that a C# programmer will understand your code if it's written as shown above than if you cram it into a module and let the compiler handle everything.


Note that like at least one other answer has alluded to, VB.NET is a fully object-oriented language. That means, among other things, that everything is an object. Even "global" variables have to be defined within an instance of a class because they are objects as well. Any time you feel the need to use global variables in an object-oriented language, that a sign you need to rethink your design. If you're just making the switch to object-oriented programming, it's more than worth your while to stop and learn some of the basic patterns before entrenching yourself any further into writing code.

like image 162
Cody Gray Avatar answered Sep 23 '22 05:09

Cody Gray


Pretty much the same way that you always have, with "Modules" instead of classes and just use "Public" instead of the old "Global" keyword:

Public Module Module1     Public Foo As Integer End Module 
like image 45
RBarryYoung Avatar answered Sep 23 '22 05:09

RBarryYoung