VBA vs. VS – Part 3: What is new in VB.Net?

I mentioned in my last blog, VBA vs. VS – Part 2: Programming Paradigm Review that VB.Net is now an object oriented (OO) programming language.  So what OO features have been introduced to VB.Net programming language?


VB.Net supports inheritance; it allows us to derive classes from an existing base class by extending the properties and methods of the base class. They can also override inherited methods with new implementations.

Class Base
    Sub Method1()
        MsgBox("This is a method in the base class.")
    End Sub
    Overridable Sub Method2()
        MsgBox("This is an override method in the base class.")
    End Sub
End Class

Class Override
    Inherits Base
    Public Field2 As Integer
    Overrides Sub Method2()
        MsgBox("This method is overrided in a derived class.")
    End Sub
End Class

Protected Sub Test()
    Dim O1 As New Base
    Dim O2 As New Override
End Sub


Overloading is the ability to create several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function.

Overloads Sub Print(ByVal theChar As Char)
    ‘ Add code that displays Char data.
End Sub
Overloads Sub Print(ByVal theInteger As Integer)
    ‘ Add code that displays Integer data.
End Sub
Overloads Sub Print(ByVal theDouble As Double)
    ‘ Add code that displays Double data.
End Sub


Overrides allows derived classes to override member variable or functions that are inherited from parent class. 

Const BonusRate As Decimal = 1.45D
Const CommissionRate As Decimal = 14.75D

Class Commission
    Overridable Function Payment( _
        ByVal FaceAmount As Decimal, _
        ByVal CommissionRate As Decimal) _
        As Decimal

        Payment = FaceAmount * CommissionRate
    End Function
End Class

Class BonusCommission
    Inherits Commission
    Overrides Function Payment( _
        ByVal FaceAmount As Decimal, _
        ByVal CommissionRate As Decimal) _
        As Decimal

        ‘ The following code calls the original method in the base
        ‘ class, and then modifies the returned value.
        Payment = MyBase.Payment(FaceAmount, CommissionRate) * BonusRate
    End Function
End Class

Sub RunCommission()
    Dim CommissionItem As Commission = New Commission
    Dim BonusCommissionItem As New BonusCommission
    Dim FaceAmount As Decimal = 40000D

    MsgBox("Normal pay is: " & _
        CommissionItem.Payment(FaceAmount, CommissionRate))
    MsgBox("Pay with bonus is: " & _
        BonusCommissionItem.Payment(FaceAmount, CommissionRate))
End Sub

Constructors and Destructors

Constructors are functions that control initialization of new instances of a class. Conversely, destructors are methods that free system resources when a class leaves scope or is set to Nothing.  VB.Net supports constructors and destructors using the Sub New and Sub Finalize procedures.


Interfaces determine what properties and methods of the classes can be consumed within the system.  It is a logical view of the classes, which provide no implementations.

Interface IAsset
    Event ComittedChange(ByVal Success As Boolean)
    Property AccountBalance() As String
    Function GetBalance() As Double
End Interface

Class InvestmentAccount
    Implements IAsset

    Public Event ComittedChange(ByVal Success As Boolean) _
       Implements IAsset.ComittedChange

    Private AccountBalanceValue As String

    Public Property AccountBalance() As String _
        Implements IAsset.AccountBalance

            Return AccountBalanceValue
        End Get
        Set(ByVal value As String)
            AccountBalanceValue = value
            RaiseEvent ComittedChange(True)
        End Set
    End Property

    Private BalanceValue As Double

    Public Function GetBalance() As Double _
        Implements IAsset.GetBalance

        Return BalanceValue
    End Function

    Public Sub New(ByVal AccountBalance As String, ByVal Balance As Integer)
        Me.AccountBalanceValue = AccountBalance
        Me.BalanceValue = Balance
    End Sub
End Class


Delegate is a reference to function and it is particular useful if we want to raise events that can call different event handlers under different circumstances.  We can dynamically associate event handlers with events by creating a delegate when we use the AddHandler statement. At run time, the delegate forwards calls to the appropriate event handler.  Delegate is not limited for event handlers and we can use it to call different versions of functions at run time.

Delegate Sub MyFirstDelegate(ByVal x As Integer)

Sub TestSub(ByVal x As Integer)
MsgBox("The value of x is: " & CStr(x))
End Sub
End Class

Protected Sub DelegateTest()
Dim T1 As New TestClass
Dim msd As MyFirstDelegate= AddressOf T1.TestSub
End Sub

Shared members

Shared members are properties, procedures, and fields that are shared by all instances of a class or structure.

Public Class SharedClass
    Public Shared Count As Integer = 1
    Public Shared Sub ShareMethod()
        MsgBox("Current value of Count: " & Count)
    End Sub

    Public Sub New(ByVal Name As String)
        Me.SerialNumber = Count
        Me.Name = Name
        Count += 1
    End Sub
    Public SerialNumber As Integer
    Public Name As String
    Public Sub InstanceMethod()
        MsgBox("Information in the first object: " & _
            Me.SerialNumber & vbTab & Me.Name)
    End Sub
End Class

Sub TestShared()
    Dim Shared1 As New SharedClass("keyboard")
    Dim Shared2 As New SharedClass("monitor")

End Sub


There are many non OO related enhancements that were added to VB.Net, e.g. structured error handling, multithreading.  However, I am not going to cover them in this article.  So far, I covered most of the OO features in VB.Net and I the examples are not so difficult to follow.  But in case if you have any question, please feel free to ask.

I often tell people that the OO programming is easy to pick up; the challenge is OO design.  How can we design a class library that is simple to maintain and easy to reuse?  It would take us years before we can master the OO design.  But at the end, it is worth the efforts!

Andrew Chan is the owner and founder of ALG Inc.

We help you to make better and faster decisions!

About Andrew Chan
Andrew Chan is an Business Consultant who gives you accurate, consistent and timely information so that you can make better and faster decisions. He is an Associate of Society of Actuaries with over 20 years of IT experience. Apart from strong analytical skills and proven technical background, he was also a former system director at Manulife who had extensive project management experience. If you are looking for someone to gather, consolidate, validate, visualize and analyze data, look no further! Andrew can provide the most cost effective business analytics solution so that you can explore, optimize, predict and visualize your business. Don’t guess on any decision, no matter it is finance, operation, marketing or sales! Always ask for evidence!

6 Responses to VBA vs. VS – Part 3: What is new in VB.Net?

  1. Pingback: Happy 17 Birthday Visual Basic for Applications! « Technologies and your business

  2. Pingback: VBA vs. VS – Part 1: Performance Review « Technologies and your business

  3. Pingback: VBA vs. VS – Part 2: Programming Paradigm Review « Technologies and your business

  4. Pingback: VBA vs. VS – Part 4: .Net Framework « Technologies and your business

  5. Pingback: VBA vs. VS – Part 5: First Excel Project « Technologies and your business

  6. Pingback: VBA vs. VS – Final: To Migrate or NOT To Migrate « Technologies and your business

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: