structure
in vb6, a structure is declared using the type...end type construction. the structure and its members all default to public access. explicit access declaration is optional. the following example shows a valid structure declaration:
type employee
id as integer
firstname as string
lastname as string
end type
in vb7, the type statement is not supported. you must declare structures using the structure ... end structure construction. every member of a structure must have an access modifier, which can be public, protected, friend, protected friend, or private. you can also use the dim statement, which defaults to public access. the structure in the preceding example can be declared as follows:
structure employee
public id as integer ' must declare access, even if public.
dim firstname as string ' still defaults to public access.
private lastname as string ' can be made private inside structure.
end structure
vb7 unifies the syntax for structures and classes. structures support most of the features of classes, including methods. structures can serve as light-weight classes that are cheap to create. however, structures do not support inheritance.
some other changes
the following are some important changes that can ease your migration from vb6.
option explicit is imposed by default
you must now declare a variable before using it.
declare and assign in the same line
you can now declare and assign a variable in the same line. a long-awaited feature.
dim myvar as integer = 9
multiple variable declaration
in vb6, you can declare variables of different types in the same statement, but you must specify the data type of each variable or it defaults to variant. the following example shows multiple declarations and their resulting data types:
dim i, j as integer ' i is variant, j is integer.
dim l as integer, m as integer ' l is integer, m is integer.
dim n as integer, x as double ' n is integer, x is double.
in vb7, you can declare multiple variables of the same data type without having to repeat the type keyword. the declarations equivalent to those in the preceding example are as follows:
dim i ' i is object.
dim j as integer ' j is integer.
' -- or --
dim i as object, j as integer ' i is object, j is integer.
dim l, m as integer ' l is integer, m is integer.
dim n as integer, x as double ' n is integer, x is double.
external procedure declaration
in visual basic 6.0, when you declare a reference to an external procedure with the declare statement, you can specify as any for the data type of any of the parameters and for the return type. the as any keyword disables type checking and allow any data type to be passed in or returned.
vb7 does not support the any keyword. in a declare statement, you must specifically declare the data type of every parameter and of the return. this improves type safety. you can overload your procedure declaration to accommodate various return types.
variable scopes
in vb6, any variable declared inside a procedure has procedure scope, so it can be accessed anywhere else within the same procedure. if the variable is declared inside a block -- that is, a set of statements terminated by an end, loop, or next statement -- the variable is still accessible from outside the block. the following example illustrates procedure scope:
for i = 1 to 10
dim n as long ' n has procedure scope although
' it was declared within a block.
n = n + incr(i)
next
w = base ^ n ' n is still visible outside the block
in vb7, a variable declared inside a block has block scope, and it is not accessible from outside the block. the preceding example can be rewritten as follows:
dim n as long ' n is declared outside the block
' and has procedure scope.
for i = 1 to 10
n = n + incr(i)
next
w = base ^ n ' n is visible outside the block but i is not.
however, the lifetime of a variable is still that of the entire procedure. this is true whether the variable has block scope or procedure scope. if you declare a variable inside a block, and if you enter that block several times during the lifetime of the procedure, you should initialize the variable to avoid unexpected values.
static local variables