Question:
Visual Basic: how to declare a variable that can be used in the entire project?
christen kae
2012-11-29 13:37:26 UTC
I know how to declare a variable that can be used throughout the entire form. But what if I have several forms in one program? Is there a way to declare a variable ONCE and it can be used in ALL of the forms?
Four answers:
2012-11-29 14:37:12 UTC
1. Create a new project for the variable declaration experiment by clicking "File" and "New Project." Choose the "Console Application" option, since it is the simplest to create a basic program in.

2. Declare a variable by pasting the following code:Dim s as StringThis defines a String (a collection of letters, numbers and punctuation marks). Notice, variable definitions begin with the "dim" keyword, are followed by the variable name, followed by "as" and the data type to be used.This example merely declares the variable; it does not initialize it. Attempting to use this variable now would produce an error.

3. Declare and initialize a variable:Dim s as String = "Hello."This is the same as the previous command, except s is given an initial value of "Hello" as soon as it is declared.

4. Paste the following shortcut:Dim s = "Hello."When assigning an initial value, or initializing the variable, it is not necessary to specify a type for the variable using the "as" command. However, it is generally a good idea to do so, simply to increase the readability of the code.
2016-10-16 10:57:06 UTC
DIM myVariable as glide (or int or in spite of) Do your self a prefer and study a genuine language like C# or Java. (DIM - comes from "measurement" which has no which potential anymore... that's a relic from the previous days, and a thoroughly incorrect term for affirming a variable.) In sq., you're saying "declare"... I declare, that makes so lots extra experience. And no, you could use the "textual content fabric" belongings of something... you prefer a variety, no longer a string. seen elementary helps you to be lazy and it will straight away convert that string to a variety, yet that facilitates the person to form some thing like "eighteen p.c." which won't be able to be switched over to a variety, inflicting your software to crash by way of fact of person enter, that's an extremely undesirable difficulty. susceptible typing is an extremely undesirable difficulty actual, and VB is the only greatest source of undesirable code interior the international, quite often for that reason.
mmarrero
2012-11-29 14:10:24 UTC
In VB6, I prefer creating a module called mGlobal.bas, named mGlobal, and create all your global variables there, like, public imeverywhere as long, and reference it, mglobal.imeverywhere



You can also reference controls on any initialized form as if they were globals, and conrols can be invisible. For example, something like, form1.label1.caption="hello world!".
?
2012-11-29 13:41:49 UTC
You'll want to use a constant. A constant is a variable you can call from every class and method. You'll have to set a constant when you define it, you can't change it afterwards. You can put it in a class called "Constants".



http://msdn.microsoft.com/en-us/library/hdb31eza(v=vs.80).aspx


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...