VB.NET Class

Array Class Collections File String Console Data Directive Enum Exception If Interface Lambda Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows

Class

Programs are complex. A Class is one part of a program. It is self-contained. When we modify a Class, other parts of the program are not affected. This makes programs more reliable and easier to develop.

Example

First this program introduces an Example Class. In the Class, we have a Private field of type Integer. We also have a constructor—the New() Sub. And finally we have the Value() Function, which returns an expression based on a field.

Next:In the Main entry point, an instance of Example is created and Value() is invoked. An Example now exists on the managed heap.

Based on:

.NET 4.5

Program that uses Class: VB.NET

Class Example
    Private _value As Integer

    Public Sub New()
	_value = 2
    End Sub

    Public Function Value() As Integer
	Return _value * 2
    End Function
End Class

Module Module1
    Sub Main()
	Dim x As Example = New Example()
	Console.WriteLine(x.Value())
    End Sub
End Module

Output

4

Most members of a Class should be Private—this improves information hiding, which leads to higher software quality. Public members can be used in unexpected ways. New() Subs and certain Functions should be Public.

MyClass

New keyword, constructor invocation

The MyClass keyword indicates exactly which variable we want to specify. This means that "MyClass._name" refers to a field with of identifier "_name". The identifier "_name" alone could mean a local variable or even a Function.

And:This example uses a formal argument in the Class constructor (Sub New). It receives the String and stores it in the field.

Tip:Often classes will have argument validation—for example, a method could reject certain String arguments.

Program that uses MyClass: VB.NET

Class Perl
    Private _name As String

    Public Sub New(ByVal name As String)
	MyClass._name = name

	Console.WriteLine(MyClass._name)
	Console.WriteLine(name)
	Console.WriteLine(_name)
    End Sub
End Class

Module Module1
    Sub Main()
	Dim p As Perl = New Perl("Sam")
    End Sub
End Module

Output

Sam
Sam
Sam

Inherits

Class shapes

With Inherits, one class can inherit from another class. This means it gains all the fields and procedures from the parent class. Let's begin by looking at Class A: this class contains a field (_value) as well as a Sub (Display()).

Next:Class B and Class C both use the Inherits keyword and are derived from Class A. They provide their own constructors (New).

Tip:You can see the New B() and New C() will do slightly different things when called.

Program that uses Inherits keyword: VB.NET

Class A
    Public _value As Integer

    Public Sub Display()
	Console.WriteLine(_value)
    End Sub
End Class

Class B : Inherits A
    Public Sub New(ByVal value As Integer)
	MyBase._value = value
    End Sub
End Class

Class C : Inherits A
    Public Sub New(ByVal value As Integer)
	MyBase._value = value * 2
    End Sub
End Class

Module Module1
    Sub Main()
	Dim b As B = New B(5)
	b.Display()

	Dim c As C = New C(5)
	c.Display()
    End Sub
End Module

Output

5
10
Syntax

In programming languages, a name such as "MyBase._value" is called a composite name. In VB.NET, this means we are accessing the base class (A) and the field on the base class (_value).

Tip:This eliminates any possible ambiguity between fields. Two classes can have fields with identical names.

Base keyword

Call base Sub. The Display Sub is defined only on Class A. However, Class B and Class C inherit this Sub as well as the field. This means that when b.Display() and c.Display() are called, the A.Display Sub is invoked.

Interfaces. One option to consider instead of the Inherits keyword is an Interface. You can use the Implements keyword to specify an Interface. Conceptually, an Interface is a contract, a set of demands that compliant types fill.

Interface

And:A base class, meanwhile, is a core template of data and functionality. Base classes are not the same as interfaces.

Note:With Inherits, we implement complex object models that can closely represent, in a declarative way, a type framework.

MustInherit

Note

The VB.NET language provides a MustInherit keyword. This provides an alternative to the Interface type. It modifies a Class so that it can only be used as a base Class. The class no longer can be directly instantiated.

And:A MustInherit Class is essentially a template that is part of the classes that inherit from it.

MustInherit

Shared

Static

Some fields in a Class are not tied to a Class instance. Only one instance of these fields needs to exist. The Shared modifier is used on fields to make one field shared among all Class instances.

Tip:A Public Shared field can be used in the same way as a global variable. This is useful for storing settings in your VB.NET program.

Shared
Program that uses Shared field: VB.NET

Class Test
    Public Shared _v As Integer
End Class

Module Module1
    Sub Main()
	Test._v = 1
	Console.WriteLine(Test._v)
	Test._v = 2
	Console.WriteLine(Test._v)
    End Sub
End Module

Output

1
2

Shared Sub

Sub keyword

Shared subroutines are separate, in the same way that a shared field is not tied to a Class instance. A Shared Sub is called with a composite name. Next, the Write Sub inside the Test class is called with "Test.Write()".

Program that uses Shared Sub: VB.NET

Class Test
    Public Shared Sub Write()
	Console.WriteLine("Shared Sub called")
    End Sub
End Class

Module Module1
    Sub Main()
	Test.Write()
    End Sub
End Module

Output

Shared Sub called

Is, IsNot

Is keyword

How can you use the VB.NET "Is" and "IsNot" operators to check reference types? With these, you can check reference types against special value such as Nothing, as we demonstrate in this quick example.

In the .NET Framework, we usually compare reference types to Nothing (null) or use member functions. Therefore, the "Is" and "IsNot" operators are most often used with the Nothing constant in VB.NET.

NothingNothing

Here:In this example, we see how "IsNot Nothing" and "Is Nothing" are evaluated with a local variable.

Tip:This pattern of code is sometimes useful. It helps if you are not sure the variable is set to something.

Program that uses Is, IsNot operators: VB.NET

Module Module1
    Sub Main()
	Dim value As String = "cat"

	' Check if it is NOT Nothing.
	If value IsNot Nothing Then
	    Console.WriteLine(1)
	End If

	' Change to Nothing.
	value = Nothing

	' Check if it IS Nothing.
	If value Is Nothing Then
	    Console.WriteLine(2)
	End If

	' This isn't reached.
	If value IsNot Nothing Then
	    Console.WriteLine(3)
	End If
    End Sub
End Module

Output

1
2
Concept

Requires reference types. These operators can only be used with reference types. The Nothing constant is a special instance of a reference type. Unlike in the C# language, you cannot use the Is-operator to perform casting.

Note:These are most commonly used with the Nothing constant. But any two references can be compared.

And:The result depends on the memory locations—not the object data the references point to.

VarType

Var keyword

The VB.NET language provides the VarType function. This function is equivalent to the GetType function. It returns a Type reference for an object instance. We provide further examples for VarType.

VarType

Summary

The VB.NET programming language

Classes are essential to VB.NET programs. They are the building blocks of our programs. A Class is a reference type—it is allocated on the managed heap. It can have Functions and Subs, and also data members—this includes fields.

VB.NET