VBA vs. VS – Part 3: What is new in VB.Net?
September 16, 2010 6 Comments
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?
Inheritance
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
O1.Method1()
O1.Method2()
O2.Method1()
O2.Method2()
End Sub
Overloading
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
Overriding
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
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
Get
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
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)
TestClass
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
msd.Invoke(10)
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")
Shared1.InstanceMethod()
Shared2.InstanceMethod()
SharedClass.ShareMethod()
End Sub
Conclusion
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!
- Happy 17 Birthday Visual Basic for Applications!
-
VBA vs. VS – Part 1: Performance Review
-
VBA vs. VS – Part 2: Programming Paradigm Review
-
VBA vs. VS – Part 3: What is new in VB.Net?
-
VBA vs. VS – Part 4: .Net Framework
- VBA vs. VS – Part 5: First Excel Project
- VBA vs. VS – Final: To Migrate or NOT To Migrate
Andrew Chan is the owner and founder of ALG Inc.
We help you to make better and faster decisions!
Pingback: Happy 17 Birthday Visual Basic for Applications! « Technologies and your business
Pingback: VBA vs. VS – Part 1: Performance Review « Technologies and your business
Pingback: VBA vs. VS – Part 2: Programming Paradigm Review « Technologies and your business
Pingback: VBA vs. VS – Part 4: .Net Framework « Technologies and your business
Pingback: VBA vs. VS – Part 5: First Excel Project « Technologies and your business
Pingback: VBA vs. VS – Final: To Migrate or NOT To Migrate « Technologies and your business