How to: Define a Class in VB.NET

Class is an abstract representation of an entity. The object is a concrete manifestation of a class. The object is the structure that contains data and methods of handling data. An object contains data through fields and properties:

  • Field: stores data and is similar to a variable;
  • Property: retrieves and changes the value of a field with Get and Set procedures; data are isolated and the values of fields are validated.

An object contains methods that are implemented actions. The methods of an object have the form of procedures.
An action recognized by an object called the event […].
A class can implement interfaces in all aspects defined in these ones. Interface is a collection of programming elements and does not contain the implementation.
Types of classes depending on the source definition:

  • Included in VB.NET;
  • Built in other applications;
  • Built or updated by developer within application.

Class acts as a data type and the developer can define variables of this type. The variables are called objects, containing data and actions.
As example, consider the class of Task objects, with the following fields:

  • cDenTask: task name;
  • cTipTask: task category;
  • cResTask: name of responsible task;
  • cPrioTask: category of task priority.

Fields cDenTask, cTipTask and cResTask are defined by String, and Boolean field is cPrioTask.
All fields are defined with Private modifier […], which means that access is restricted to source code developed in the class. Accessing and modifying the fields are made through properties DenTask, TipTask, ResTask and PrioTask.

Public Class Task
    Private cDenTask As String
    Private cTipTask As String
    Private cResTask As String
    Private cPrioTask As Boolean

   'Properties class Task

   'property field cDenTask
    Public Property DenTask()
            Return cDenTask
        End Get
        Set(ByVal value)
            cDenTask = value
        End Set
    End Property

   'property field cTipTask
    Public Property TipTask()
            Return cTipTask
        End Get
        Set(ByVal value)
            cTipTask = value
        End Set
    End Property

    'property field cResTask
    Public Property ResTask()
            Return cResTask
        End Get
        Set(ByVal value)
            cResTask = value
        End Set
    End Property

    'proprerty field cPrioTask
    Public Property PrioTask()
            Return cPrioTask
        End Get
        Set(ByVal value)
            cPrioTask = value
        End Set
    End Property

    'function in class Task
    Public Function ModificaPrioritate(ByVal prioNou As Boolean) As String
        cPrioTask = prioNou
        Dim res As String
        res = "It was modified the task priority for value " &_
cPrioTask & " !"
        Return res
    End Function

    ' example constructor without parameters
    Public Sub New()
        cDenTask = "New Task"
        cTipTask = "Type New Task"
        cResTask = "Responsible New Task"
        cPrioTask = False
    End Sub

    'overloading constructor - version constructor with parameters
    Public Sub New(ByVal denT As String, ByVal tipT As String, _
ByVal resT As String, ByVal prioT As Boolean)
        cDenTask = denT
        cTipTask = tipT
        cResTask = resT
        cPrioTask = prioT
    End Sub
End Class

Besides the fields and properties, the class Task defined above contains a method called ModificaPrioritate built as a procedure of type Function and two overloaded constructor methods.
Constructor method is special, being implemented by the procedure Sub New. Code of constructor method is executed before the rest of the code defined in the class of objects. Constructor method is called to create the object.
When the objects are not used, they are destroyed through the reference-tracing garbage collection system. Destruction of objects is based on special method called destructor and implemented by the procedure Sub Finalize. Unlike the constructor method, destructor is not called explicitly, the system dealing with automatic dialing of destructor.