Inheritance with VB.NET 2005 (Page 1 of 6 )
This is the beginning of a new series dedicated to explaining inheritance in VB.NET 2005. This article gives you an introduction to inheritance. The upcoming articles will cover some of the advanced aspects of Visual Basic.NET 2005.
A downloadable file for this article is available
here.
The entire source code for this article is available in the form of downloadable zip. The solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Enterprise Edition. Even though I believe that the source code available with this contribution can work with Microsoft Visual Studio.NET 2003/2002, I didn't really test it in any other environment. I request that you post in the discussion area if you have any problems with the code's execution.
Developing a simple class
My previous articles and series already covered a lot of ground concerning classes, members, fields, methods, properties and so on. If you are very new to the concept of classes, I suggest you go through my previous series before proceeding with this one.
For the sake of simplicity in explanation, I developed a simple class as follows:
Public Class First
Private m_x As Integer
Private m_y As Integer
Public Property X() As Integer
Get
Return m_x
End Get
Set(ByVal value As Integer)
m_x = value
End Set
End Property
Public Property Y() As Integer
Get
Return m_y
End Get
Set(ByVal value As Integer)
m_y = value
End Set
End Property
End Class
The above class simply contains two fields (m_x and m_y) and two properties (X, Y). As the fields (m_x and m_y) are defined as private, they are viewable/accessible only within the class "First." The two properties are mainly used to provide a way to interface with the two fields.
To work with the above class, you may design a form with a single button and label. The code for the button would be as follows:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As New First
With obj
.X = 10
.Y = 20
Me.Label1.Text = .X + .Y
End With
End Sub
Within the code above, you can observe that I am creating an object named "obj" based on the class "First." I am assigning values to the properties and retrieving the values from the same, for calculation.
Next: Basic inheritance >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee