VB.NET Classes

VB.NET,OOP,VB.NET Class Declaration,Fields,Constructor,Constructor Overloading,Properties,Methods,Self Referencing,
Share it:
VB.NET Classes
google

Introduction


Object-Oriented Programming (OOP) is a programming model where functions belong to classes. Unlike VB6, for instance, where stand-alone functions may be declared and used across a project, every function in VB.NET must belong to a class. A function that belongs to a certain class is called a method. Apart from methods, a class may also include data that the methods operate on and produce some results. A class is responsible for a specific task. The word class itself is used as a blueprint. When you have to use a class, you must initialize it first, which is known as making an object or instance of the class. As an analogy, the word bus is a class (blue-print) whereas a specific bus is the object or instance of the bus class. In essence, a class is a user-defined type that may include fieldsproperties, and methods, each of which will be detailed in the context of a class later in the article.

OOP makes it a lot easier to model large and complex systems as you use classes to represent real-world entities.


VB.NET Class Declaration


Let's begin by understanding how classes are declared or defined in VB.NET, which is based on the OOP model. In VB.NET, a class is declared by using the Class keyword as shown below.


'Blue-print of the Bus class
Public Class Bus

 'Fields
    Private m_Name As String
    Private m_Model As Integer

 'Constructor
    Public Sub New(ByVal name As String, ByVal m_Model As Integer)
        m_Name = name
        Me.m_Model = m_Model
    End Sub

 'Overloaded constructor
    Public Sub New(ByVal m_Model)
        Me.New("Default bus name", m_Model)
    End Sub
    
    'Property
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Model = value
        End Set
    End Property

 'Property
    Public Property Model() As Integer
        Get
            Return m_Model
        End Get
        Set(ByVal value As Integer)
            If value <> 0 Then
                m_Model = value
            End If
        End Set
    End Property

 'Method
    Public Sub Drive(ByVal km As Integer)
        'Code not shown for simplicity  
    End Sub
End Class
We will explore classes in VB.NET using the Bus class, declared above.

Fields

Fields are internal data that pertain to a class. In this case, The Bus class has two fields that hold information about a given bus i.e. its name (a pet name) and its model, respectively. These two fields are shown below. There is no limit on the number of fields a class may have and they vary from one class to another as fields are class-specific. A class that represents a person, for instance, may have fields that hold information about a person's name, age, address and so on.
'Fields of the Bus class
    Private m_Name As String
    Private m_Model As Integer

Constructor

When we have an object of the Bus class, we would expect that the specific object has information about its name and model (as the object represents a certain bus). But where does the object gets this information from? The information is fed into the object when the object is created. The role of a constructor in a class is the same: it forces any entity, that wants to create an object of the class, to provide any necessary information. An example of this is shown below where an object, called bus1, of the Bus class, is created.
'Constructor of the Bus class
    Public Sub New(ByVal name As String, ByVal m_Model As Integer)
        m_Name = name
        Me.m_Model = m_Model
    End Sub
    
    'Creating an object of the Bus class
    Dim bus1 As New Bus("This is bus1", 72346)
    Console.WriteLine("My name is " & bus1.Name)

Constructor Overloading

A class can have multiple constructors, which is known as constructor overloading. A constructor does not have a name (unlike properties or methods e.g.) - they are all declared with the New keyword, but requires that each and every constructor has a unique set of parameters. The Bus class has two constructors, both of which are shown below for ease. It means that Bus objects can be created in two different ways resulting in two different kinds of objects. If you use the first constructor, you must give it a name and model parameters. If you use the second constructor, you only need to give it a model as the name will be assigned to it automatically or, in other words, the name is hard-coded. But note that all objects created via this constructor will have the exact same name! Note also that our second constructor calls the first one under the hood to populate the fields and thereby making the job easier.
'The default constructor
    Public Sub New(ByVal name As String, ByVal m_Model As Integer)
        m_Name = name
        Me.m_Model = m_Model
    End Sub

 'The overloaded constructor
    Public Sub New(ByVal m_Model)
        Me.New("Default bus name", m_Model)
    End Sub

Properties

The fields of an object of a class can be set to new values (if allowed) during the lifetime of the object. The fields can be made public (so that anyone can access them) and we are done! But there is a problem with this solution because we do not have any control over the values that the fields get set to as they would get set directly. This is problematic because we end up with objects having illegal values set to their fields. As an example, it does not make much sense if the model field of an object of the Bus class is set to 0 or -2536. We should prevent this when someone tries to do this and this is where the properties come into the picture. Properties in a class give us control over the values that it's fields are set to. Properties are what you have to contact if you want to get the value of a field or set it to another value.
 Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Model = value
        End Set
    End Property

    Public Property Model() As Integer
        Get
            Return m_Model
        End Get
        Set(ByVal value As Integer)
         'Filter illegal values for the m_Model field
            If value > 0 Then
                m_Model = value
            End If
        End Set
    End Property
In the .NET framework, we need one property defined per field. This property both gets the value of the field as well allows you to set another value to it (this is in contrast to some programming languages e.g. Java where you have to define a getter and setter method-pair for each field). The two fields in our Bus class have two properties (Name and Model) defined, one for each.

Methods

Having data (in fields) in a class is not of much use if you can not take action on it and get some results. Methods are used to process or act on data in some way. Our Bus defines a single method (others not shown for simplicity) Drive. As the name implies, the result of invoking this method is the bus being driven km (this comes as a parameter to the method) kilometers.
 Public Sub Drive(ByVal km As Integer)
        'Code not shown for simplicity  
    End Sub

Self Referencing

Sometimes you want to name the fields in a class and parameters in its constructor the same because it's easier to remember what a particular variable is. I have given an example of this in the first default constructor above where the names of the field m_Model and the corresponding incoming parameter m_Model are the same. In such cases, you have to use the Me keyword when you assign the incoming parameter to the corresponding field for the correct behavior. Otherwise, the incoming parameter would not be assigned to the field, as you may think. Rather, the incoming parameter gets assigned to itself as it is in the scope of constructor! When you use the Me keyword, however, you are telling the VB.NET compiler that it should assign the incoming parameter in the constructor to the field that belongs to the class. These two behaviors are shown below.
'Using the Me keyword
    Public Sub New(ByVal name As String, ByVal m_Model As Integer)
        m_Name = name
        Me.m_Model = m_Model
    End Sub
    
    Dim bus As New Bus("Bus", 2342)
    Console.WriteLine(bus.Model)
The result of calling the Model property on the bus object above is 2342, which is correct.
'Not using the Me keyword
    Public Sub New(ByVal name As String, ByVal m_Model As Integer)
     m_Name = name
     'The parameter gets assigned to itself as it is in scope
     m_Model = m_Model
    End Sub
    
    Dim bus As New Bus("Bus", 2342)
    Console.WriteLine(bus.Model) 'Prints 0, which is wrong!


The result of calling the Model property on the bus object above is 0, which is incorrect! Once again, this is because we do not use the Me keyword in the constructor where the m_Model field (returned by the Model property) gets assigned. Self-reference is not only limited to constructors. You can use it in the entire class (properties, methods, etc). The Me keyword, in general, refers to the class.

                                        Share this article with your friends
Share it:

VB.NET

VB.NET Classes

Post A Comment:

0 comments: