|
A namespace is a section of code that is recognized
with a name. A namespace allows a programmer or a group of
programmers who work with others to create code with names that don't
conflict with code of the other programmers or other groups.
As mentioned already, a namespace is a section of code.
Therefore, it must be created in a file before being used.
|
The formula to create a namespace is:
Namespace name
End Namespace
As you can see, the creation of a namespace starts with the Namespace
keyword and ends with the End Namespace expression. On the right side of
the starting Namespace keyword, enter a name. The name follows the rules we have
used so far for names in the Visual Basic language. Here is an example:
Namespace Census
End Namespace
The section between the starting Namespace line and End
Namespace is the body of the namespace. In this body, you can write code as
you want. For example, you can create a class in it. Here is an example:
Namespace Geometry
Public Class Rectangle
End Class
End Namespace
Of course, in the body of the class, you can write the
necessary code. Here is an example:
Namespace Geometry
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
Accessing Members of a Namespace
|
|
The variables, procedures, and classes created inside a
namespace can be referred to as its members.
To access a member of a namespace, you can
use the period operator. To do
this, where you want to access the member, type the name of the namespace, followed by a
period, followed by the desired member of the namespace. Here is an example:
Namespace Geometry
Public Class Rectangle
End Class
End Namespace
Module Exercise
Public Function Main() As Integer
Geometry.Rectangle
Return 0
End Function
End Module
In the same way, wherever you want to access a member of a
namespace, you can simply qualify it. Once the member has been qualified, you
can use it as we saw in previous lessons. Here are examples:
Namespace Geometry
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
Module Exercise
Public Function Main() As Integer
Dim Rect As Geometry.Rectangle
Rect = New Geometry.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, you can create as many members as you want
in the body of a namespace.
In the above code, we create our namespace in the same file
that contains the Main() function. To better manage your code, you can
create your namespaces in different files. Here is an example:
Namespace Geometry
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
Public Class Triangle
Public Base As Double
Public Height As Double
Public Function Area() As Double
Return Base * Height / 2
End Function
End Class
End Namespace
In the same way, you can create as many namespaces in as
many files as you want. A namespace can be created in more than one file.
When accessing each member of a namespace, you can qualify
its name wherever you want to use it. Here are examples that use the above
namespace that was created in a separate file:
Module Exercise
Public Function Main() As Integer
Dim Rect As Geometry.Rectangle = New Geometry.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()))
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()))
Return 0
End Function
End Module
Instead of qualifying each member, you can import the
namespace in the file where you want to use it. To do this, in the top section
of the file, type Imports followed by the name of the namespace.
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:
- You can create as many namespaces as you want inside of a nesting
namespace
- You can nest as many namespaces in as many nesting namespaces
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
Built-In Classes: The Object Class |
|
The .NET Framework is a huge library made of
various classes and constants you can directly use in your application
without necessarily explicitly loading an external library. To start, this main
library provides a class called Object.
When you create a class, it
automatically inherits its primary characteristics from the parent of all
classes: Object.
Equality of Two Class Variables
|
|
When you declare and initialize two variables, one of
the operations you may want to subsequently perform is to compare their
value. To support this operation, the Object class provides its
children with a method called Equals. The Equals() method
comes in two versions. The first has the following syntax:
Public Overridable Function Equals(obj As Object) As Boolean
This version allows you to call the Equals()
method on a declared variable and pass the other variable as argument.
Here is an example:
Imports System
Module Exercise
Public Function Main() As Integer
' First book
Dim NumberOfPages1 As Integer = 422
' Second book
Dim NumberOfPages2 As Integer = 858
' Third book
Dim NumberOfPages3 As Integer = 422
If NumberOfPages1.Equals(NumberOfPages2) = True Then
Console.WriteLine("The first and the second books " & _
"have the same number of pages")
Else
Console.WriteLine("The first and the second books " & _
"have different number of pages")
End If
If NumberOfPages1.Equals(NumberOfPages3) = True Then
Console.WriteLine("The first and the third books " & _
"have the same number of pages")
Else
Console.WriteLine("The first and the third books " & _
"have different number of pages")
End If
Return 0
End Function
End Module
This would produce:
The first and the second books have different number of pages
The first and the third books have the same number of pages
The first version of the Object.Equals method is
declared as Overridable, which means you can override it if you create
your own class. The second version of the Object.Equals() method
is:
Public Shared Function Equals(objA As Object, objB As Object) As Boolean
As a Shared method, to use it, you can pass the
variables of the two classes whose values you want to compare.
In both cases, if the values of the variables are
similar, the Equals() method returns true. If they are different,
the method returns false. If you are using the Equals() method to compare
the variables of two primitive types, the comparison should be straight
forward. If you want to use this methods on variables declared from your
own class, you should provide your own implementation of this method.
In previous lessons, we learned that, to convert the
value of a variable declared from a primitive type to a string, you could
call the ToString() function. Here is an example:
Imports System
Module Exercise
Public Function Main() As Integer
Dim NumberOfPages As Integer = 422
Console.Write("Number of Pages: ")
Console.WriteLine(NumberOfPages.ToString())
Return 0
End Function
End Module
In many programming languages, programmers
usually have to overload an (extractor) operator to display the value(s)
of class' variable to the screen. The Object class provides an
alternative to this somewhat complicated solution, through the ToString()
method. It syntax is:
Public Overridable Function ToString As String
Although the Object class provides this method
as non abstract, its implemented version is more useful if you use a
primitive type such as Integer, Double and their variances or a string
variable. The best way to rely on it consists of overriding it in your own
class if you desire to use its role.
When we study inheritance, we will learn that all data types
used in a C# program are "based on" an object called object. As
introduced earlier, you can use this data type to declare a variable that would
hold any type of value. Because this is some type of a "universal"
data type, it can also be initialized with any value. Here are examples:
Imports System
Module Exercise
Public Function Main() As Integer
Dim Number As Object
Dim Thing As Object
Number = 244
Thing = "Professor Kabba"
Console.WriteLine(Number)
Console.WriteLine(Thing)
Return 0
End Function
End Module
This would produce:
244
Professor Kabba
As you can see, when an object variable is initialized, the
compiler finds out the type of value that was assigned to it. This is referred
to as boxing. This mechanism is transparently done in Visual Basic.
If you declare a variable using a primitive data type (Integer,
Single, Double, etc), at one time, you may be interested in converting the value
of that variable into an Object. Here is an
example:
Imports System
Module Exercise
Public Function Main() As Integer
Dim Number As Integer = 244
Dim Thing As Object = Number
Number = 244
Thing = Number
Console.WriteLine(Number)
Console.WriteLine(Thing)
Return 0
End Function
End Module
This would produce:
244
244
This operation is referred to as unboxing. As you can see,
this operation is performed transparently.
While a constructor, created for each class, is used
to instantiate a class. The Object class provides the Finalize()
method as a type of destructor.
Built-In Classes: The Random Class
|
|
Imagine you have a series of numbers, such these: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, and 20. Imagine you want to select one of these numbers, any of them. A number is referred to as random if it has been selected from a pool without a specific pattern to follow. For example, if you decide to select the value 17 from this list, if there was an exact reason that number was selected, then it is not considered random. In reality, it is difficult for a number to qualify as random. For this reason, most random numbers are referred to as pseudo-random.
To support the ability to create or choose a random number, the .NET Framework provides the
Random class. To start, you can declare a variable of this class, using one of its two constructors. Here is an example that uses the default constructor:
Module Exercise
Public Function Main() As Integer
Dim rndNumbers As Random = New Random
Return 0
End Function
End Module
After declaring the variable, you can start getting numbers from it. To do this, you call the
Next() method, which is overloaded in three versions. One of the versions of this method takes no argument and its syntax is:
Public Overridable Function Next As Integer
This method generates a randomly selected integer between 0 and the
MinValue value of the Integer data type. Here is an example:
REM Imports System
Module Exercise
Public Function Main() As Integer
Dim rndNumbers As Random = New Random
Dim rndNumber As Integer = rndNumbers.Next()
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Return 0
End Function
End Module
Here is an example of running the program:
Number: 1369872590
Press any key to continue . . .
In the same way, you can call this version of the Next() method repeatedly to get random. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim nbr As Integer
Dim rndNumber As Integer = 0
Dim rndNumbers As Random = New Random
For nbr = 1 To 9
rndNumber = rndNumbers.Next()
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Next
Return 0
End Function
End Module
Here is an example of running the program:
Number: 1924504148
Number: 1257846191
Number: 424740120
Number: 1009211682
Number: 544356245
Number: 708951978
Number: 759684741
Number: 1325535324
Press any key to continue . . .
The Seed of a Random Number
|
|
Consider the following program:
Module Exercise
Public Function Main() As Integer
Dim rndNumbers As Random = New Random
Dim rndNumber As Integer = rndNumbers.Next()
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Return 0
End Function
End Module
Here is an example of running the program:
Number: 573991745
Press any key to continue . . .
Here is another example of running the same program:
Number: 334223329
Press any key to continue . . .
Notice that the numbers generated are different. When creating a program that repeatedly gets a series of random numbers, you may (or may not) want the Random class to generate the same number over and over again. A seed is a constant value that controls whether a random generation would produce the same result every time it occurs. For example, using a seed, you can impose it upon the Random class to generate the same number every time the Next() method is called.
To support the ability to use a seed, the Random class is equipped with a second constructor whose syntax is:
Public Sub New(Seed As Integer)
Based on this, to specify a seed, when declaring a Random variable, pass a constant integer to the constructor. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim rndNumbers As Random = New Random(20)
Dim rndNumber As Integer = rndNumbers.Next()
Console.WriteLine("Number: {0}", rndNumber)
Return 0
End Function
End Module
Here is one example of running the program:
Number: 375271809
Press any key to continue . . .
Here is another example of running the same program:
Number: 375271809
Press any key to continue . . .
Notice that the numbers are the same. Consider this program also:
REM Imports System
Module Exercise
Public Function Main() As Integer
Dim nbr As Integer
Dim rndNumber As Integer = 0
Dim rndNumbers As Random = New Random(20)
For nbr = 1 To 5
rndNumber = rndNumbers.Next()
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Next
Return 0
End Function
End Module
Here is one example of running the program:
Number: 375271809
Number: 1472524622
Number: 1605850688
Number: 1776011503
Press any key to continue . . .
Here is another example of running the same program:
Number: 375271809
Number: 1472524622
Number: 1605850688
Number: 1776011503
Press any key to continue . . .
Notice that the sequences are the same. In both cases, this indicates that, if you specify a seed, the Random class would generate the same number or the same sequence of numbers.
Generating Random Numbers in a Range of Numbers
|
|
So far, we have been using any number that would fit an integer. In some assignments, you may want to restrict the range of numbers that can be extracted. Fortunately, the Random class allows this.
Using the Random class, you can generate random positive numbers up to a maximum of your choice. To support this, the Random class is equipped with another version of the
Next() method whose syntax is:
Public Overridable Function Next(maxValue As Integer) As Integer
The argument to pass to the method determines the highest integer that can be generated by the
Next() method. The method returns an integer. Here is an example that generates
random numbers from 0 to 20:
REM Imports System
Module Exercise
Public Function Main() As Integer
Dim nbr As Integer
Dim rndNumber As Integer = 0
Dim rndNumbers As Random = New Random
For nbr = 1 To 9
rndNumber = rndNumbers.Next(20)
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Next
Return 0
End Function
End Module
Here is an example of running the program:
Number: 1
Number: 7
Number: 1
Number: 16
Number: 14
Number: 19
Number: 3
Number: 1
Press any key to continue . . .
The above version of the Next() method generates numbers starting at 0. If you want, you can specify the minimum and the maximum range of numbers that the
Next() method must work with. To support this, the Random class is equipped with one more version of this method and that takes two arguments. Its syntax is:
Public Overridable Function Next(minValue As Integer, _
maxValue As Integer) As Integer
The first argument specifies the lowest value that can come from the range. The second argument holds the highest value that the
Next() method can generate. Therefore, the method would operate between both values. Here is an example that generates random numbers from 6 to 18:
Module Exercise
Public Function Main() As Integer
Dim nbr As Integer
Dim rndNumber As Integer = 0
Dim rndNumbers As Random = New Random
For nbr = 1 To 9
rndNumber = rndNumbers.Next(6, 18)
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Next
Return 0
End Function
End Module
Here is an example of running the program:
Number: 17
Number: 9
Number: 8
Number: 15
Number: 10
Number: 9
Number: 13
Number: 11
Press any key to continue . . .
When you initialize a variable using the New operator, you
are in fact asking the compiler to provide you some memory space in the heap
memory. The compiler is said to "allocate" memory for your variable.
When that variable is no longer needed, for example when your program closes, it
(the variable) must be removed from memory and the space it was using can be
made available to other variables or other programs. This is referred to as
garbage collection.
The .NET Framework solves the problem of garbage collection
by letting the compiler "clean" memory after you. This is done
automatically when the compiler judges it necessary so that the programmer
doesn't need to worry about this issue.
When you declare a variable based on a class, the
compiler allocates memory for it. This portion of memory would be
available and used while the program is running and as long as the
compiler judges this necessary. While the program is running, the instance
of the class uses or consumes the computer's resources as necessary. When
the object is not needed anymore, for example when the program terminates,
the object must be destroyed, the memory it was using must be emptied to
be made available to other programs on the computer, and the resources
that the object was using should (must) be freed to be restored to the
computer so they can be used by other programs. To make this possible, the
Object class is equipped with a method called Finalize that is
protected and therefore made available to all descendant classes of the
.NET Framework.
The syntax of the Object.Finalize() method is:
Overrides Protected Sub Finalize()
The Finalize() method is automatically called
when an instance of a class is not needed anymore. In all of the
classes we have used so far, this method was transparently called when the
compiler judged that the instance of the class was not used anymore. If
you don't want this method to be called, call the
Public Shared Sub SuppressFinalize(ByVal obj As Object)
In most cases, you can let the compiler call the
Finalize() method when the time comes.