Inheritance with VB.NET 2005 - Dealing with constructors during inheritance
(Page 5 of 6 )
I already introduced constructors in my previous series of articles covering OOPS using Visual Basic 2005. If you are very new to constructors, I strongly suggest you go through my previous series before continuing this section.
During inheritance, the constructors are executed starting from the highest super class through the lowest sub class in the form of a hierarchy. Now, let us modify our hierarchy of classes as follows:
Public Class First
Private m_x As Integer
Private m_y As Integer
Public Sub New()
m_x = 10
m_y = 10
End Sub
...
End Class
Public Class Second
Inherits First
Private m_z As Integer
Public Sub New()
m_z = 20
End Sub
...
End Class
Public Class Third
Inherits Second
Public Function getProduct() As Integer
Dim s As Integer
s = Me.X * Me.Y * Me.Z
Return s
End Function
...
End Class
Now that you have modified the hierarchy as above, let us try to modify the testing code as well:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As New Third
With obj
Me.Label1.Text = "Sum = " & .getSum & ", Product = " & .getProduct
End With
End Sub
Now, you can observe from the above code that I didn't assign any values to the properties during testing. I just created the object and all the values, which are default values, have been assigned during the execution of constructors at various levels.
Next: Dealing with constructors with parameters during inheritance >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee