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.

Creating a Namespace

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:

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.

Importing a Namespace

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:

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:

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.

The System Namespace

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:

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

Introduction

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.

Stringing a Class

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:

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.

Boxing and Un-Boxing

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:

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.

Finalizing a Variable

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

Introduction

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.

Getting a Random Number

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:

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:

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:

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:

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.

Class Finalization

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.

Custom Libraries

Introduction

If you cannot find a function you want, you can create one and be able to use it.
If you plan to use such a function over and over again in different programs, you can
create a library and store the function in it. You can even create one or a series of commercial functions
and be able to distribute or
sell it through a library.

A library is a program that contains procedures and/or other
resources that other programs can use. Such a program is created with the same
approach as the programs we have created so far. Because a library is not an
executable, it does not need the Main() procedure. A library usually has the
extension .dll.

Creating a Library

A library can be made of a single file or as many
files as necessary. A file that is part of a library can contain one or
more procedures (or classes). Each procedure (or class) should implement a behavior that can eventually be
useful and accessible to other procedures (or classes). The contents of a library
are created exactly like those we have used so far. Everything depends on how
you compile it.

To create a library:

On the main menu of Microsoft Visual Studio, you can click File -> New ->
Project

On the Start Page, on the right side of Create, you can click Project

On the Standard toolbar, you can click the New Project button or click the arrow
of the New Project button and click New Project

In the Templates list of the New Project dialog box, click
Class Library. Accept or specify the name and click OK. Once the starting
project has been generated, write the necessary code. To compile it, on the main
menu, click Build -> Build Solution. If you were working from the command
prompt, you would execute the following command:

vbc /target:library NameOfFile.vb

After doing this, a library with the name
of the file and the extension .dll would be created. If you use the above
technique, the new library would be created using the name of the file. If you
are working from the Command Prompt, to compile the project and create a custom name,
use the following syntax:

vbc /target:library /out:DesiredNameOfLibrary.dll NameOfFile.vb

Here is an example:

vbc /target:library /out:Arithmetic.dll exo.vb

Practical
Learning: Creating a Library

Start Microsoft Visual Studio

To start a new project, on the main menu, click File -> New ->
Project...

In the Project Types list, make sure Visual Basic is selected.
In the Templates List, click Class Library

To create the library, on the main menu, click Build -> Build
Arithmetic

To start another project, on the main menu, click File -> New ->
Project...

In the Templates List, click Console Application

Change the Name to Algebra and click OK

In Windows Explorer or My Documents, locate the Arithmetic folder that
contains the library project. Access a sub-folder of the same name, followed
by the bin sub-folder, and followed by the Debug sub-folder

Right-click Arithmetic.dll and click Copy

Still in Windows Explorer or My Documents, locate the Algebra folder that
contains the current project. Right-click the sub-folder of the same name
and click Paste

In the Solution Explorer, right-click Algebra and click Add Reference...

In the Add Reference dialog box, click the Browse tab

Use the Look In combo box to locate and select a sub-folder named Algebra
in the current project

Click Arithmetic.dll and click OK

In the Solution Explorer, right-click Module1.vb and click Rename

Type Exercise.vb and press Enter. Accept to make all necessary
changes