|< Day Day Up >|
Declarations and Names
A Visual Basic .NET program contains declarations . A declaration is any statement that defines something new in a program, whether that new thing is a local variable, a method, a class, a namespace, and so on. When something new is declared in a program, it must be given a name . Names can be made up of any combination of letters , numbers , and the underscore character ( _ ). Some examples of names follow.
Dim x As Integer Dim _y, __z As Integer Dim Choice1, Choice2, Choice3 As Integer
There are two restrictions on names in Visual Basic .NET: First, a name cannot begin with a number, and, second, a name that begins with an underscore must contain at least one other character. The second restriction allows the compiler to distinguish identifiers from line continuations. For example, the second local variable declaration in the following example is not valid in Visual Basic .NET.
Dim x As Integer Dim _ As Integer ' Error: _ is not a valid name _ = 5 x = _
As can be seen from the example, if it were valid to have a name that was just an underscore, it would be extremely difficult to tell the name and a line continuation apart.
Some declarations, such as classes and namespaces, can contain other declarations. For example:
Module Test Sub Main() Dim x As Integer End Sub End Module
In this example, the declaration of the module Test contains the declaration of the subroutine Main . The declaration of the subroutine Main contains the declaration of the local variable x . In general, declarations can only be referred to by name within the bounds of the containing declaration (including any nested declarations) ”this defines the scope in which the name can be used. For example:
Namespace A Namespace B Class C End Class End Namespace End Namespace Module Test Sub Main() Dim x As C ' Error: C is not found. End Sub End Module
In this example, the scope of the class C is the containing namespace declaration B . Since the subroutine Main is not within the scope of class C 's name, it can't refer to C .
In most cases, it is possible to refer to a name outside of its scope by qualifying the name with the name of its scope. This is done by putting a period and the name of the scope before the name you want. The name on the right side of the period then refers to a name that is within the scope of the declaration named on the left side of the period. For example:
Namespace A Namespace B Class C End Class End Namespace End Namespace Module Test Sub Main() Dim x As A.B.C End Sub End Module
In this example, the subroutine Main is within namespace A 's scope. The namespace C is qualified with the name of namespace B , which is further qualified with the namespace A , which is in scope. This allows Main to refer to the class C .
Two declarations in the same scope cannot use the same name. If they did, it might be impossible to determine which of the two declarations was being referred to. For example:
Module Test Sub Main() Dim x As Integer Dim x As Long ' Invalid because x is already declared x = 5 End Sub End Module
In this case, there is no way to tell which local variable x is being assigned to. To avoid this kind of confusion, two declarations cannot use the same name (the one exception to this is method overloading, discussed in Chapter 10, Methods ). Remember, however, that two declarations can use the same name if they are declared within separate scopes.
Module Test Sub Test() Dim x As Long End Sub Sub Main() Dim x As Integer End Sub End Module
In this case, the two local variables named x are declared in different methods, so their names do not conflict.
Keep in mind that this is only a brief overview of how declarations and names work in Visual Basic .NET ”there are exceptions that apply to certain kinds of declarations, and these special rules will be covered later in the book.
When a Visual Basic .NET program is compiled, the entire source of the program is compiled together at the same time. This means that each source file can see the declarations made in all source files. Also, all the declarations within a file are processed at once, so there is no need for header files or forward references as in C or C++. This means that one declaration can refer to another declaration that occurs after it in a source file. For example, the following code compiles and runs fine, even though the method Add is declared after the method Multiply that calls it.
Function Multiply(ByVal x As Integer, ByVal y As Integer) As Integer Dim Result As Integer For Count As Integer = 1 to x Result = Add(Result, y) Next Count Return Result End Function Function Add(ByVal x As Integer, ByVal y As Integer) As Integer Return x + y End Function
As a result, the organization of source code is entirely up to the programmer and is never dictated by the code itself. Where code goes in a program is completely a matter of programmer preference.
|< Day Day Up >|