VB.Net

VB.Net Classes & Objects

VB.Net Classes & Objects

When you define a class, you're essentially creating a blueprint for a data type. This does not describe any data, but it does define what the class name signifies, that is, what a class object will consist of and what operations may be performed on such an object.

Objects are subclasses of a class. Members of a class are the methods and variables that make up the class.

Class Definition

A class definition begins with the keyword Class, is followed by the class name, and is concluded with the End Class statement. The general form of a class definition is as follows: 

[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _

Class name [ ( Of typelist ) ]

[ Inherits classname ]

[ Implements interfacenames ]

[ statements ]

End Class

Where,

  • attributelist is the list of attributes that apply to the class. Optional.
  • accessmodifier defines the class access levels, it can take values as - Public, Protected, Protected Friend, Friend, and Private. Optional.
  • Shadows indicate that the variable re-declares and hides an element with the same name, or a set of overloaded elements, in a base class.  
  • MustInherit states that the class can only be used as a base class and that you cannot directly build an object from it, i.e., it is an abstract class. Optional.
  • NotInheritable indicates that the class is ineligible to be utilized as a base class.

• Partial: denotes a class definition that is only partially complete.

• Inherits:indicate the base class from which it is descended.

• Implements:provides the interfaces from which the class derives.

Member Functions and Encapsulation

A member function of a class is a function that, like any other variable, has its definition or prototype within the class declaration. It can act on any object of the class to which it belongs and has access to all the members of that class for that object.

Member variables are (from a design standpoint) attributes of an object that are kept private to implement encapsulation. These variables are only accessible through the public member functions.

Constructors and Destructors

A class constructor is a special member Sub of a class which is called whenever new objects of that class are created. A constructor’s name is New, and it has no return type. A default constructor does not have any parameters, although a constructor can have parameters if necessary. These constructors are known as parameterized constructors. This technique allows you to assign an object's initial value at the time of its creation. 

A destructor is a special member Sub of a class called whenever an object of that class exits its scope. “Finalize” is the name of a destructor, and it can neither return a value nor take any parameters. Destructors can be very useful for releasing resources before exiting the program, such as shutting files, releasing memories, and so on. Destructors cannot be passed down or overloaded.

Shared Members of a VB.Net Class

Using the ‘Shared’ keyword, we may define class members as static. When we declare a class member as Shared, no matter how many class objects are generated, only one copy of the member exists.

The keyword ‘Shared’ signifies that a class has just one instance of the member. Shared variables are used to define constants since their values can be retrieved by executing the class without creating an example.

Shared variables can be set up outside of the scope of a member function or class definition. Shared variables can also be initialized within the class definition.

A member function can alternatively be declared as Shared. Such functions can access only Shared variables. Shared functions occur before the creation of the object.

Inheritance

The concept of inheritance is one of the most significant in object-oriented programming. Inheritance enables us to define one class in terms of another, making it easier to design and manage applications. This also allows for the reuse of code functionality and a quick implementation time.

Instead of developing entirely new data members and member functions when creating a class, the programmer can specify that the new class should inherit the members of an existing class. The old class is known as the base class, and the new class is known as the derived class.

Base & Derived Classes

A class can be derived from many base classes or interfaces, which means it can inherit data and functionalities from multiple base classes or interfaces.

The syntax for generating derived classes in VB.Net is as follows:

<access-specifier> Class <base_class>

...

End Class

Class <derived_class>: Inherits <base_class>

...

End Class

Base Class Initialization

The base class's member variables and member methods are passed down to the derived class. As a result, the superclass object should be generated before the subclass. In VB.Net, the superclass or base class is known as ‘MyBase’.

VB.Net supports multiple inheritances.

Top course recommendations for you

    Functions in Python
    1 hrs
    Beginner
    18K+ Learners
    4.46  (1136)
    GitHub Tutorial for Beginners
    2 hrs
    Beginner
    20.8K+ Learners
    4.45  (1480)
    Trees in Java
    2 hrs
    Beginner
    8.3K+ Learners
    4.54  (241)
    C for Beginners
    2 hrs
    Beginner
    156.6K+ Learners
    4.49  (16545)
    Binary Trees
    2 hrs
    Intermediate
    5.6K+ Learners
    4.6  (233)
    PowerPoint for Beginners
    2 hrs
    Beginner
    72.8K+ Learners
    4.53  (5236)
    UI / UX for Beginners
    1 hrs
    Beginner
    325.7K+ Learners
    4.53  (35418)
    Splunk Tutorial
    1 hrs
    Beginner
    4.2K+ Learners
    4.5  (330)
    Turbo C++
    1 hrs
    Beginner
    16.2K+ Learners
    4.41  (722)
    Dev C++ Tutorial
    1 hrs
    Beginner
    6.4K+ Learners
    4.45  (247)