This tutorial covers the use of the VB.NET Classes.
What are VB.NET Classes?
A class is used to define the characteristics of an object (fields and properties) and the things it can do (methods).
The simplest definition for a class is that it is used a blueprint or model for creating objects.
Other Terminology
An Instance is the class object that is created at run time. It can be called the physical manifestation of a class. An instance is created when a class is assigned to a variable.
The terms object and instance are very interchangeable here, so expect to hear both. The best way to describe is that an object is the actual thing made using the blueprint (class) and the instance is a virtual copy of that object.
Methods are another important term. A method is a function that belongs to an object. This will be more clear later on as we define methods for our classes.
A class is defined with the class
keyword, and it’s closed using the End Class statement.
Public Class Student
.
.
.
End Class
Understanding Scope and Variables
- Global Scope: A variable declared in the main VB.NET body. Accessible from any part of the code and any kind of scope.
- Local Scope: A variable declared in a function or method. Due to encapsulation, this variable can only be accessed in the function or method in which it was declared in.
You can declare variables in three different settings in VB.NET classes. Each of one these has it’s own protection level which determines what can access it.
Public:
Equivalent to the Global Scope. Can be accessed from any part of the code.Private:
Equivalent to the Local Scope. Can only be accessed within the method it was declared in.Protected:
Similar to private variables, with one key difference. If a child class inherits a class with protected variables, the child class will be able to access these protected variables.
Constructor
A constructor is a special kind of method that VB.NET calls when it creates an object/instance. VB.NET uses the constructor to perform tasks such as initializing any variables that the object will need when it starts. It is created by using the following code.
Class Human
Public Sub New()
'Instructions to be executed
End Sub
End Class
The New()
function is what defines it as a constructor. The sub
keyword shows that it is a method, and the Public
keyword defines it as a public function.
Creating a Data Class
First we’ll create a class without any methods in it. Such classes can be referred to as Data Classes. Note the placement of the Class. It is placed between the Module
Keyword and the Main()
function.
Module Module1
Class Person
Public name As String
Public age As Integer
Public RollNo As String
End Class
Sub Main()
Dim obj As Person = New Person() 'Creating a class object
'Assigning values to object variables
obj.name = "Sam"
obj.age = 17
obj.RollNo = "1234"
Console.WriteLine(obj.name)
Console.WriteLine(obj.age)
Console.WriteLine(obj.RollNo)
Console.Read()
End Sub
End Module
Sam
17
1234
To access a variable belonging to a class, you simply have to use the following format, objectname.variablename
. In the above example, obj is the name of the object, and name is the variable. So we end up with obj.name
.
Destructor
A destructor is a special procedure of a class that is executed whenever an object of its class is destroyed. Some properties of the destructor are listed below.
- Cannot return any value or take any parameters
- Cannot be inherited
- Uses the function
Finalize()
.
Inheritance
Note: VB.NET supports multiple inheritance. This means a class that inherits another class, may further be inherited by another class. So Class A is inherited by Class B, and Class B is inherited by Class C.
Class Student: Inherits Teacher
The above command will inherit the Teacher class into the Student Class. This will transfer all non-private members (attributes and methods) of the Teacher class into the Student Class. More on Inheritance in a separate Article.
This marks the end of the VB.NET classes article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be asked below in the comment section.