landingnormand
Member
- Joined
- Mar 26, 2009
- Messages
- 15
- Programming Experience
- Beginner
(I'm pretty new to OOP so please bear with me... )
I have a base class (called "One" for example) that has a number of public properties (e.g. Name, etc).
To create an instance of the One class I call a method in a separate controller class for example:
The GetOne method returns a One object populated with data (i.e. its properties are all set to some value e.g. Name = "joe" etc) and I can use it thus:
I now wish to create a derived class "Two" that inherits from class One but that has some additional properties (e.g. IsSomething, etc.)
What should Class Two look like, and how do I create an instance of class Two and ensure that all the properties of class One get populated?
I was thinking that Class Two should perhaps look like:
And I'd create an instance of it thus:
Is this the correct way of doing this i.e. do I have to call the GetOne() method in ClassTwo to populate the base class?
Thanks,
Norman
I have a base class (called "One" for example) that has a number of public properties (e.g. Name, etc).
To create an instance of the One class I call a method in a separate controller class for example:
VB.NET:
Dim objController as New Controller
Dim objOne as One = objController.GetOne(id) ' where id is a variable of type integer
The GetOne method returns a One object populated with data (i.e. its properties are all set to some value e.g. Name = "joe" etc) and I can use it thus:
VB.NET:
If Not objOne Is Nothing Then
Textbox1.Text = objOne.Name
End If
I now wish to create a derived class "Two" that inherits from class One but that has some additional properties (e.g. IsSomething, etc.)
What should Class Two look like, and how do I create an instance of class Two and ensure that all the properties of class One get populated?
I was thinking that Class Two should perhaps look like:
VB.NET:
Public Class Two
Inherits One
Private _IsSomething as Boolean
Private _One as One
Public ReadOnly Property IsSomething() As Boolean
Get
Return _IsSomething
End Get
End Property
Public ReadOnly Property TwoOne() As One
Get
Return _One
End Get
End Property
Public Sub New(ByVal Id As Integer)
Dim objController as New Controller
_One = objController.GetOne(id)
_IsSomething = True ' for example
End Sub
End Class
VB.NET:
Dim objTwo as New Two(id)
If Not objTwo Is Nothing Then
Textbox1.Text = objTwo.TwoOne.Name
CheckBox1.Checked = objTwo.IsSomething
End If
Is this the correct way of doing this i.e. do I have to call the GetOne() method in ClassTwo to populate the base class?
Thanks,
Norman