VB.NET Variables
Variables are fundamental to any programming language, and Visual Basic .NET (VB.NET) is no exception. A variable is a symbolic name given to a location in memory that stores a value. This value can be changed during the execution of a program.
Declaring Variables
Before you can use a variable, you must declare it. Variable declaration involves specifying the variable's name and its data type. The data type tells the compiler how much memory to allocate for the variable and what kind of data it can hold. The general syntax for declaring a variable is:
Dim variableName As DataType
Dim
: This keyword is used to declare a variable.variableName
: This is the name you choose for your variable. It must start with a letter or an underscore and can contain letters, numbers, and underscores. It should be descriptive.As
: This keyword separates the variable name from its data type.DataType
: This specifies the type of data the variable will hold (e.g.,Integer
,String
,Boolean
,Double
).
Common Data Types
Data Type | Description | Example Size |
---|---|---|
Boolean |
True or False values. | 1 byte |
Byte |
Unsigned integer from 0 to 255. | 1 byte |
Char |
Single character. | 2 bytes |
Date |
Date and time values. | 8 bytes |
Decimal |
Large numbers with decimal points. | 16 bytes |
Double |
Floating-point numbers with large range and precision. | 8 bytes |
Integer |
Signed whole numbers. | 4 bytes |
Long |
Signed whole numbers with a larger range than Integer. | 8 bytes |
Short |
Signed whole numbers with a smaller range than Integer. | 2 bytes |
Single |
Floating-point numbers with smaller range and precision than Double. | 4 bytes |
String |
Sequence of characters. | Variable (dependent on string length) |
Object |
Can hold any type of data. | 4 bytes plus overhead |
Example Declarations
Dim age As Integer
Dim userName As String
Dim isActive As Boolean
Dim price As Decimal
Dim today As Date
Initializing Variables
You can assign a value to a variable when you declare it. This is called initialization. If you don't initialize a variable, it will have a default value based on its data type (e.g., 0 for numeric types, False
for Boolean
, Nothing
for objects).
Dim count As Integer = 0
Dim message As String = "Hello, World!"
Dim isComplete As Boolean = True
Dim pi As Double = 3.14159
You can also declare and initialize multiple variables of the same type on a single line:
Dim x As Integer = 10, y As Integer = 20, z As Integer = 30
Assigning Values to Variables
After a variable has been declared, you can assign or reassign values to it using the assignment operator (=
).
Dim score As Integer
score = 100 ' Assigning a value
score = score + 10 ' Reassigning with an updated value
Console.WriteLine("Current score: " & score) ' Output: Current score: 110
Variable Scope
Variable scope refers to the region of the program where a variable is accessible. In VB.NET, variables can have different scopes:
- Local Variables: Declared within a procedure (like a Sub or Function). They are only accessible within that procedure.
- Module-Level Variables: Declared at the top of a module, outside of any procedure. They are accessible by all procedures within that module.
- Class-Level Variables: Declared within a class, outside of any procedure. They are accessible by all procedures and methods within that class. If declared with
Private
, they are only accessible within the class. If declared withPublic
, they are accessible from outside the class.
Implicit and Explicit Conversions
VB.NET supports both implicit (widening) and explicit (narrowing) data type conversions.
Implicit Conversion Example:
Dim myInteger As Integer = 100
Dim myLong As Long = myInteger ' Implicitly converts Integer to Long
Explicit Conversion Example:
Dim myDouble As Double = 123.45
Dim myInteger As Integer
' Explicitly convert Double to Integer (data loss occurs here)
myInteger = CInt(myDouble)
Console.WriteLine(myInteger) ' Output: 123
Common conversion functions include CInt()
, CLng()
, CDbl()
, CStr()
, etc.