Variable DeclarationsVisual Basic does not require you to specifically declare a variable before it is used. If a variable is not declared, Visual Basic creates the variable by using a default data type usually a variant. A variant can contain any type of information. Using a variant for general information has two major drawbacks; it can waste memory resources, and the variable type might be invalid for use with functions that expect a specific variable type. Always declaring your variables before they are used is good programming practice. Therefore, you should now take a look at the two ways to declare a variable in Visual Basic, explicit and implicit declarations, and the special case of fixed-length strings. Explicit DeclarationExplicit declaration means that you use statements to define the names and types of your program's variables. These statements do not assign values to the variables but merely tell Visual Basic what the variables should be called and what type of data they can contain. You can use each of the following statements to explicitly declare a variable's type: Dim varname[As vartype][, varname2 [As vartype2]] Dim, Private, Static, and Public are Visual Basic keywords that define how and where the variable can be used. (You learn more about where these keywords can be used in upcoming sections.) varname and varname2 represent the names of two variables that you want to declare. As indicated in the syntax, you can specify multiple variables in the same statement as long as you separate the variables with commas. Note that the syntax shows only two variables on one line, but you can specify several. In fact, over a thousand characters can fit on one line in the Code window. From a practical standpoint, however, you should refrain from writing lines of code that are wider than the displayed Code window. This way, you make your code much easier to read because you don't have to scroll left and right when looking at it. vartype and vartype2 represent the data types of the respective variables. A data type is a keyword that tells Visual Basic what kind of information is stored in the variable. As indicated, the variable type is an optional property. If you include the variable type, you must include the keyword As. If you do not include a variable type, the default type (usually Variant type) is used. The following code shows the use of these declaration statements for actual variables: Private nNumVal As Integer Private nAvgVal As Integer, vInptVal As Variant Static fClcAverage As Single Dim sFirstName As String Implicit DeclarationDeclaring your variables using the Dim or other statements shown in the preceding section is best, but in many cases you can also assign a type to a variable using an implicit declaration. With this type of declaration, a special character is used at the end of the variable name when the variable is first assigned a value. The characters for each variable type are shown in Table 8.3. Table 8.3 - Special Variable Type Characters
Visual Basic automatically sets aside space for implicitly declared variables the first time each variable is encountered. The variables that were declared using the code in the preceding section could have been used as implicitly declared variables, as follows: nNumVal% = 0 nAvgVal% = 1 vInptVal = 5 fClcAverage! = 10.1 sFirstName$ = "Lauren" Notice that the variable vInptVal doesn't have a declaration character. This means that vInptVal will be of the Variant type. |
|||||||||||||||||||||||||
![]() |
![]() |
|||