![]() |
Click here to advertise with us
|
|
|||||||
| VB.NET General Discussion VB.NET general discussion area |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
(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: Code:
Dim objController as New Controller Dim objOne as One = objController.GetOne(id) ' where id is a variable of type integer Code:
If Not objOne Is Nothing Then Textbox1.Text = objOne.Name End If 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: Code:
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
Code:
Dim objTwo as New Two(id) If Not objTwo Is Nothing Then Textbox1.Text = objTwo.TwoOne.Name CheckBox1.Checked = objTwo.IsSomething End If Thanks, Norman |
|
||||
|
No, that's not correct. You're combining inheritance and composition there. Inheritance is an IS A relationship while composition is a HAS A relationship.
Consider this. A Car IS A Vehicle, while a Car HAS A SteeringWheel. The Car class inherits the Vehicle class so every Car is a Vehicle. The Car class is, in part, composed of a SteeringWheel so every Car has a SteeringWheel. Now, in your code, this part says that Two IS A One: Code:
Inherits One Code:
Public ReadOnly Property TwoOne() As One Code:
Public Class LaptopComputer
Inherits Computer
If a Two IS A One then a Two object must have its own Name property because a One object has a Name property and a Two object IS A One object.
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms MSDN: Data Walkthroughs | "How Do I?" Videos My Blog: Custom Events | Dynamic GDI+ Drawing |
|
|||
|
Thanks for the reply. I follow your example, but in practical terms I know I'm a bit lost or at least I know I have more studying to do!
Quote:
|
|
||||
|
Quote:
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms MSDN: Data Walkthroughs | "How Do I?" Videos My Blog: Custom Events | Dynamic GDI+ Drawing |
|
|||
|
Thanks for the replies (jmcilhinney and cjard), I really appreciate the time taken to respond. I certainly see the power and flexibility of OOP - it will just take (me!) some time to structure things correctly.
[cjard] I have one finally question for the moment that I hope you could help me with. In your example involving Computer, Laptop and Server I see that in the Constructor for Laptop and Server you set some properties. Imagine that the values for these properties (e.g. BatteryMilliAmpereHours and RaidArraySize) are coming from a database and that you had an additional class that was derived from Laptop (as opposed to Computer - I'm not sure if this is a valid case but anyway...). Is it common/good practice for each layer/derived class to involve a call to the database? e.g. the constructor for Laptop would involve a call to the database that would get all the properties unique for Laptop and the constructor for the class that derives from Laptop would get all the properties unique to it? I can see that you'd want to have a call to the database in both depending on whether you wanted to create an instance of Laptop or your new class, but isn't creating an instance of this new class less efficient (for want of a better term) as you have two calls to the database? In other words is there a way of creating an instance of the new class (i.e. the one derived from Laptop) that involves only one call to the database? Thanks |
|
||||
|
Quote:
The purpose of a constructor is that, when it is done, the object is suitable for use in any of the contexts within which it might be found. In MVC concept theory your classes here are data containers. You would have a separate class push/pull data between them and a database Quote:
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|