Vb Object Oriented Programming

Classes and Objects

Classes are a type, and objects are examples or instances of that class.

To create a class, you only need to use the Class statement, which, like other compound statements in Visual Basic, needs to end with End Class:

Public Class DataClass
        ⋮
End Class

This creates a new class named DataClass. You can create an object of this class, data, like this—note that you must use the New keyword to create a new instance of a class:

Dim data As New DataClass()

You also can do this like this:
Dim data As DataClass = New DataClass()

Fields, Properties, Methods, and Events

Fields, Properties, Methods, and Events are called the members of a class.
They can be declared as:

  • Public: Gives variables public access, which means there are no restrictions on their accessibility.
  • Private: Gives variables private access, which means they are accessible only from within their class, including any nested procedures.
  • Protected: Gives variables protected access, which means they are accessible only from within their own class or from a class derived from that class. Note that you can use Protected only at class level (which means you can't use it inside a procedure), because you use it to declare members of a class.
  • Friend: Gives variables friend access, which means they are accessible from within the program that contains their declaration, as well as anywhere else in the same assembly.
  • Protected Friend: Gives variables both protected and friend access, which means they can be used by code in the same assembly, as well as by code in derived classes.

The fields of a class are also called the class's data members.

Example

Public Class DataClass
    Public value As Integer
End Class

You also can make fields hold constant values with Const:

Example

Public Class Class1
    Public Const Field1 As Integer = 0
        ⋮
End Class

Sometimes you might need to guard the access to the data in objects. This is achieved using properties.
Properties are retrieved and set like fields, but are handled with the Property Get and Property Set procedures, which provide more control on how values are set or returned.

Methods represent the object's built-in procedures.

Example

Public Class Animal
    Public Sub Eating()
        MsgBox("Eating...")
    End Sub

    Public Sub Sleeping()
        MsgBox("Sleeping...")
    End Sub
End Class

Events
Events allow objects to perform actions whenever a specific occurrence takes place. For example, when you click a button, a Click event occurs, and you can handle that event in an event handler, as we already have done so many times.

Example

Private Sub tracker_ThreeClick(ByVal Message As String) _
    Handles tracker.ThreeClick
    TextBox1.Text = Message
End Sub

Class vs. Object Members

Members that apply to a class and are invoked with the class name are called shared or static or class members; the members that apply to objects created from the class are called instance or object members.

To make members shared, you use the Shared keyword.

Example

Public Class Mathematics
        Shared Function Add(ByVal x As Integer, ByVal y As Integer) _
            As Integer
            Return x + y
        End Function
    End Class

Abstraction, Encapsulation, Inheritance, and Polymorphism

Abstraction: The ability to create an abstract representation of a concept in code (as an object named employee is an abstraction of a real employee).

Encapsulation: Encapsulation is all about the separation between implementation and interface. In other words, when you encapsulate an object, you make its code and data internal and no longer accessible to the outside except through a well-defined interface. This is also called data hiding.

Polymorphism: This is all about creating procedures that can operate on objects of different types. For example, if both person and employee objects have a last_name property, a polymorphic procedure can use that property of both objects. Visual Basic handles polymorphism with both late binding and multiple interfaces.

Inheritance: Inheritance allows you to derive new classes from other classes. The idea here is that if you were to create, for example, a class for a specific Visual Basic form and then derive a new type of form from that class, the derived class will inherit all the base class's functionality, even before you start adding code or customizing the new form.

Overloading, Overriding, and Shadowing

Overloaded: members provide different versions of a property or method that have the same name, but that accept a different number of parameters (or parameters of different types).

Overridden: properties and methods are used to replace an inherited property or method. When you override a member from a base class, you replace it. Overridden members must accept the same data type and number of arguments.

Shadowed: members are used to create a local version of a member that has broader scope. You also can shadow a type with any other type. For example, you can declare a property that shadows an inherited method with the same name.

Constructors and Destructors

You create objects with the New keyword, as we've seen, like this:

Dim data As New DataClass()

Creating Namespaces

When you're creating a large number of classes, it can be helpful to divide them up into their own namespaces to help organize things. Namespaces are used to create separate spaces so that names can't conflict with other names already declared. To create your own namespace, you can use the Namespace statement:

Namespace {name | name.name}
   componenttypes
End Namespace

Namespaces are always public, which means that the declaration of a namespace cannot include any access modifiers.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.