Unlike previous versions of VB, VB.NET is a fully object-oriented programming language. This object orientation has two aspects: access to the .NET Framework Class Library (FCL), and the ability to define and instantiate custom classes using inheritance.
The .NET Framework Class Library is an object-oriented library
consisting of thousands of system and application types (classes,
structures, interfaces, delegates, and enumerations) organized in
namespaces. The namespaces and types of the .NET FCL offer a wide
range of functionality, from wrappers to classic Win32 API functions
to diagnostics and debugging to input/output to data access, Windows
forms programming, and web application and web service development.
In fact, most of the “intrinsic”
functions and procedures that are part of the VB.NET language are
implemented as members of classes in the
Microsoft.VisualBasic
namespace.
In order to access the types in a namespace, the compiler must be
provided with a reference to its assembly. This is done with the
/r
switch using the VB command line compiler. When
a type is instantiated or a type member is invoked, ordinarily the
type’s fully qualified namespace must be included.
For example, a new instance of an
ApplicationException
class is generated by the
following code fragment:
Dim e As New System.ApplicationException( )
As an alternative, you can have the compiler or the Visual Studio
design-time resolve an unqualified object reference by using the
Imports
directive to import a
type’s namespace. The Imports
directive appears at the top of a file, after any
Option
statements and before any type or module
definitions. Using Imports
, we can instantiate an
ApplicationException
object as follows:
Dim e As New ApplicationException( )
In addition to accessing the functionality of the .NET FCL, you can
also define your own custom types, including custom classes. In
defining a class, you can take advantage of
inheritance; that is, you can indicate that your
class derives from another class, either from a class in the .NET FCL
or from another custom class. In this case, your class automatically
inherits the functionality of its base class. You can even take
advantage of inheritance without explicitly specifying an inherited
class. All classes automatically inherit from type
System.Object
, and all structures (which cannot
take advantage of inheritance directly) automatically inherit from
type System.ValueType
. For instance, consider the
following code:
Public Class AppClass Public Shared Sub Main Dim obj As New AppClass System.Console.WriteLine(obj.ToString) End Sub End Class
When compiled, the code produces the following output:
AppClass
The code calls the AppClass.ToString
method. But
the ToString
method isn’t
explicitly defined; instead, it is inherited from the
Object
class.
Along with inherited members, you can also access any members that you defined in your class. Any particular class member can be either an instance member or a shared member. Instance members require that an object of the class be instantiated, and that the member (and its value, if there is one) belongs to that instance. Shared members do not require an object instance, and instead can be invoked using the class itself. If a shared member has a value, it is shared across all instances of a class.
VB supports three different kinds of members: fields, properties, and methods. The following section examines each of these.
A field is simply a publicly accessible constant or variable. Because it is usually undesirable to give users of a class unrestricted access to its data, fields typically consist of constants or read-only variables. (Read-only variables are not completely read-only; values can be assigned to them when the variable is declared and initialized, as well as in the class constructor.) For example:
Public Class FieldClass Public ReadOnly Value As Integer = 100 Public Const Pi = Math.Pi Public Sub New( ) End Sub Public Sub New(x As Integer) Value = x End Sub End Class
A property provides a public interface to a
class’ state information. A property can consist of
either a get accessor (which returns the property value), a set
accessor (which sets the property value), or both. For example, the
following code defines an Age
property for the
Employee
class:
Public Class Employee Private EmployeeAge As Integer Public Property Age( ) As Integer Get Return EmployeeAge End Get Set EmployeeAge = Value End Set End Property End Class
A method represents an action or operation that
is performed upon the class. VB recognizes functions, which are
declared with the
Function
...End
Function
construct, and subroutines, which are
declared with the Sub
...End
Sub
construct. Functions return a value to the
caller; subroutines do not. Both functions and subroutines can
include a list of parameters to be passed to the procedure by the
caller. For example:
Public Class MathLib Public Sub SwapNumbers(ByRef Num1 As Integer, _ ByRef Num2 As Integer) Dim Temp As Integer Temp = Num2 Num2 = Num1 Num1 = Temp End Sub Public Function Pi( ) As Double Return System.Math.Pi End Function End Class
By default, arguments are passed to functions and subroutines
by value; that is, the function or subroutine is
provided with a copy of the argument, and any changes made to the
argument by the function or subroutine are discarded when control
returns to the caller. This behavior can be overridden, and an
argument can be passed to a function or subroutine by
reference; that is, the address of the data is provided to
the function or subroutine. To pass an argument by reference, precede
the parameter in the function or subroutine’s
parameter list with the ByRef
keyword.
You can call a subroutine in either of the following ways:
oMath.SwapNumbers(x, y) Call oMath.SwapNumbers(x, y)
You call a function as follows:
Result = oMath.Pi( )
However, if you’re not interested in the function’s return value, you can also call the function as if it were a subroutine.