Changes

Jump to: navigation, search

Declaring Visual Basic Variables and Constants

1,227 bytes added, 18:15, 1 August 2007
Assigning New Values to Visual Basic Variables
intInterestRate = 20
</pre>
 
== Referencing Variable Values ==
 
Accessing the value of a variable in Visual Basic code is as simple as typing the variable name. For example, the following code example adds 20 to the value of intInterestRate and assigns the result to the ''intNewRate'' variable:
 
<pre>
Dim intInterestRate As Integer = 10
Dim intNewRate As Integer = 0
 
intNewRate = intInterestRate + 20
</pre>
 
== Declaring and Referencing Visual Basic Constants ==
 
Constants, once declared and initialized cannot be changed (hence the name ''constant' and are declared using the Visual Basic ''Const'' keyword. The syntax for declaring variables is as follows:
 
'''Const''' ''constName'' '''As''' ''datatype'' = ''value''.
 
For example, to declare a String constant named ''companyName'' with a value of ''Techotopia'' the declaration would read as follows:
 
<pre>
Const companyName As String = "Techotopia"
</pre>
 
A Visual Basic constant is referenced using the name defined when the constant was declared. For example, the following Visual Basic code sets the ''Text'' roperty of a Label control to the string value contained in the ''companyName'' constant:
 
<pre>
Const companyName As String = "Techotopia"
 
companyLabel.Text = companyName
</pre>

Navigation menu