In the case where the variables are of different types the type of variable must be declared at the end of each group of the same type. When the variables are of the same type, the type declaration only needs to be made once at the end of the declaration: Dim intInterestRate, intExchangeRate As Integer It is also possible to declare multiple variables on the same line by separating each variable with a comma. To declare an Integer value named intInterestRate for example: Dim intInterestRate As Integer For a complete list of Visual Basic variable types see Understanding Visual Basic Variable & Constant Types. The As keyword precedes the declaration of the variable type (String, Date, Integer etc). For example, when declaring a String variable prefix the name with str (e.g. Try to use a descriptive variable name and prefix the name with something which indicates the variable type. variableName is the name assigned to the variable. In the above outline, Dim is the keyword which indicates to Visual Basic that a variable is being declared. The syntax for a simple declaration of a variable is as follows: Variables are declared using the Visual Basic Dim keyword. The value assigned to a constant, as the name suggests, cannot be changed once it has been declared an initialized. The difference between variables and constants is that the value stored in a variable can be changed at any time after it has been created. Having declared the variable you can assign a value to it and read that value anywhere in your application code. To do so you would declare a variable with a name of your choice (such as interestRate) and specify the variable as an Integer type (since it will be storing a number). For example, you might want to store the interest rate for a banking application.
#Visual basic datatype code#
These memory locations are assigned names which can be used in Visual Basic code to reference these memory locations. Both constants and variables provide a way to reserve memory locations for the storage of values of various types (such as numbers, characters and strings).
#Visual basic datatype how to#
In this chapter we will learn how to declare and initialize variables and constants in Visual Basic.īefore learning how to declare variables and constants it is worth a quick reminder of the difference between variables and constants in Visual Basic. The different types of Visual Basic variables and constants were described in detail in the previous chapter ( Understanding Visual Basic Variable and Constant Types).
#Visual basic datatype pdf#
All rights Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99 The following table lists the literal type characters.Ĭopyright © 1997-2010 Rocky Mountain Computer Consulting, Inc. For example, the following sets variable x equal to a Single value rather than the Integer which is the default for literal values with this format. You can use literal type characters to give a literal a specific type. For example, suppose you declare a variable using this code.ĭata don't recommend using these type characters because I think they make the code harder to understand but it's nice to have this table so you can understand other people's code. The following table lists Visual Basic's type characters. So here's my own version that I can hopefully find more easily. I always have trouble finding this information because the search keywords are so general they return too many hits of the wrong type. VB Helper: Tip: Visual Basic Data Type Characters