|
You can create one namespace inside of another namespace. This is referred to as nesting the namespace. To nest a namespace, create its body in the body of an existing namespace. Here is an example: Namespace Geometry
Namespace Quadrilateral
End Namespace
End Namespace
Inside the new namespace, you can implement the behavior you want. For example, you can create a class. The parent namespace is still its own namespace and can contain any necessary thing. Here is an example: Namespace Geometry
Friend Class Triangle
Public Base As Double
Public Height As Double
Public Function Area() As Double
Return Base * Height / 2
End Function
End Class
Namespace Quadrilateral
Public Class Rectangle
Public Width As Double
Public Height As Double
Public Function Perimeter() As Double
Return (Width + Height) * 2
End Function
Public Function Area() As Double
Return Width * Height
End Function
End Class
End Namespace
End Namespace
To access a member of the parent namespace, you can qualify it as we saw previously. To access a member of the nested namespace outside the parent, type the name of the nesting namespace, followed by a period, followed by the name of the nested namespace, followed by a period, and followed by the member you want to access. Here are examples: Namespace Geometry
Friend Class Triangle
Public Base As Double
Public Height As Double
Public Function Area() As Double
Return Base * Height / 2
End Function
End Class
Namespace Quadrilateral
Public Class Rectangle
Public Width As Double
Public Height As Double
Public Function Perimeter() As Double
Return (Width + Height) * 2
End Function
Public Function Area() As Double
Return Width * Height
End Function
End Class
End Namespace
End Namespace
Module Exercise
Public Function Main() As Integer
Dim Tri As Geometry.Triangle = New Geometry.Triangle
Tri.Base = 36.09
Tri.Height = 28.46
MsgBox("Triangle Characteristics" & vbCrLf & _
"Base:" & vbTab & vbTab & FormatNumber(Tri.Base) & vbCrLf & _
"Height:" & vbTab & vbTab & FormatNumber(Tri.Height) & vbCrLf & _
"Area:" & vbTab & vbTab & FormatNumber(Tri.Area()))
Dim Rect As Geometry.Quadrilateral.Rectangle
Rect = New Geometry.Quadrilateral.Rectangle
Rect.Width = 40.18
Rect.Height = 28.46
MsgBox("Rectangle Characteristics" & vbCrLf & _
"Width:" & vbTab & vbTab & FormatNumber(Rect.Width) & vbCrLf & _
"Height:" & vbTab & vbTab & FormatNumber(Rect.Height) & vbCrLf & _
"Perimeter:" & vbTab & FormatNumber(Rect.Perimeter()) & vbCrLf & _
"Area:" & vbTab & vbTab & FormatNumber(Rect.Area()))
Return 0
End Function
End Module
In the same way:
Remember how to backwardly qualify the name of a member of a nested namespace. If the namespace exists in a separate file and you want to import it, type the Imports keyword, followed by the name of the parent namespace, followed by a period, and followed by the nested namespace. In the same way, you can import any nested namespace, as long as you appropriately qualify its name.
To make programming in Visual Basic easier, many classes were created and stored in various namespaces. Each namespace is used to provide specific instructions. One of the most regularly used namespaces in the Visual Basic language is called System. Inside of the System namespace is a class called Console. The Console class is used to display things on the console screen also called the DOS window. The Console class contains procedures to display information on the screen or to retrieve information from the user who types it in the DOS window. The procedure that is used to display text on the screen is called Write. To use Write(), inside of the parentheses, type the sentence between double-quotes. Here is an example: Module Exercise
Public Function Main() As Integer
System.Console.Write("Welcome")
Return 0
End Function
End Module
Besides Write(), the Console class also provides a procedure called WriteLine(). Here is an example: Module Exercise
Public Function Main() As Integer
System.Console.WriteLine()
Return 0
End Function
End Module
The difference is that, after displaying something on the screen, the Write() method keeps the caret on the same line but WriteLine() transfers the caret to the next line. We mentioned that, to access a member of a namespace, you could type the name of the namespace, followed by a period, and followed by the name of the member. This technique is referred to as qualifying a member of a namespace. This can be long ins some cases. Instead of qualifying a member of a namespace every time you want to use it, you can import the namespace into the file where you want to access its member(s). Here is an example: Imports System
Module Exercise
Public Function Main() As Integer
Console.WriteLine()
Return 0
End Function
End Module
|
|
|||||
|
|