Question:
Creating Variables in Visual Basic?
A Person
2012-04-23 22:32:14 UTC
I want a variable and a label to be able to be used in two different subroutines, they are both private now, so how do i make variables and labels in visual basic that can be used in two private subs.

One sub is when the program loads, and the other is when a button is pressed
Three answers:
nicefx
2012-04-23 22:35:18 UTC
Declared variable as a global variable. To make a variable as a global variable, declare the variable outside the sub routines! usually typed variable declaration command below class name or module name
pramod
2012-04-23 23:22:10 UTC
Declaring Data Type



The As clause in the declaration statement allows you to define the data type or object type of the variable you are declaring. You can specify any of the following types for a variable:



An elementary data type, such as Boolean, Long, or Decimal



A composite data type, such as an array or structure



An object type, or class, defined either in your application or in another application



A .NET Framework class, such as Label or TextBox



An interface type, such as IComparable or IDisposable



You can declare several variables in one statement without having to repeat the data type. In the following statements, the variables i, j, and k are declared as type Integer, l and m as Long, and x and y as Single:



Dim i, j, k As Integer

' All three variables in the preceding statement are declared as Integer.

Dim l, m As Long, x, y As Single

' In the preceding statement, l and m are Long, x and y are Single.



For more information on data types, see Data Types in Visual Basic. For more information on objects, see Objects and Classes in Visual Basic and Programming with Components.

Local Type Inference



Type inference is used to determine the data types of local variables declared without an As clause. The compiler infers the type of the variable from the type of the initialization expression. This enables you to declare variables without explicitly stating a type. In the following example, both num1 and num2 are strongly typed as integers.

VB



Public Sub inferenceExample()



' Using explicit typing.

Dim num1 As Integer = 3



' Using local type inference.

Dim num2 = 3



End Sub





If you want to use local type inference, Option Infer must be set to On. For more information, see Local Type Inference (Visual Basic) and Option Infer Statement..

Declaring Characteristics



The lifetime of a variable is the period of time during which it is available for use. In general, a variable exists as long as the element that declares it (such as a procedure or class) continues to exist. In some cases it is possible to extend a variable's lifetime. For more information, see Lifetime in Visual Basic.



The scope of a variable is the set of all code that can refer to it without qualifying its name. A variable's scope is determined by where it is declared. Code located in a given region can use the variables defined in that region without having to qualify their names. For more information, see Scope in Visual Basic.



A variable's access level is the extent of code that has permission to access it. This is determined by the access modifier (such as Public (Visual Basic) or Private (Visual Basic)) that you use in the Dim statement. For more information, see Access Levels in Visual Basic.

See Also

Tasks

How to: Create a New Variable (Visual Basic)

How to: Create a Variable that Does Not Change in Value (Visual Basic)

How to: Move Data Into and Out of a Variable (Visual Basic)

Reference

Data Type Summary (Visual Basic)

Protected (Visual Basic)

Friend (Visual Basic)

Static (Visual Basic)

Option Infer Statement

Concepts

Deciding What Type of Variable to Define (Visual Basic)

Declared Element Characteristics (Visual Basic)

Local Type Inference (Visual Basic)
2012-04-23 22:36:33 UTC
Dim a as string



string = "HELLO"



Print a



lol simple.


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