|
-
Aug 31st, 2008, 01:54 AM
#1
Thread Starter
Lively Member
[RESOLVED] Swapping Objects between Objects and Array of Objects
I have the following configuration to create a basic vehicle with an engine.
Code:
Interface IEngine
End Interface
Class Vehicle
Dim VehicleEngine As IEngine
Public Property Engine() As IEngine
Get
Return VehicleEngine
End Get
Set(ByVal value As IEngine)
VehicleEngine = value
End Set
End Property
Sub New()
Me.Engine = New Engine
End Sub
End Class
Class Engine
Implements IEngine
End Class
This is an example of how I instantiate the object.
Code:
Dim Police as New Vehicle
This is simple also, but a question starts to come up.
Code:
Police.Engine = Nothing
In the above example I destroyed the VehicleEngine object which was created in the constructor when I created the Police object. When this code is complete, the Engine that is destroyed will first be copied into a Garage object. If you are with me so far, I have a quick question. Right now with these examples I am doing the swap manually. Create Object, Copy Object and Destroy Object. Both ways (removing engine and adding engine). Is this the best way to do this, or is there a simpler more efficient way to do the swap.
I have a second question. What would be the best way to create an array of Engines in the above code in the vehicle class instead of just the single engine. So assume that the car can have a hundred engines. What would you alter in the code above to be able to get and set a hundred different engines. I am referring to not have to create a hundred properties but to use one to manage a hundred objects. I definitely could use help with that.
-
Aug 31st, 2008, 06:45 AM
#2
Re: Swapping Objects between Objects and Array of Objects
First up, you aren't destroying anything. Let's say I'm married.Now, I get divorced:Does that mean that the woman who was my wife was destroyed? Absolutely not. It just means that my Wife property no longer refers to that woman.
The same goes for your code. The fact that you assign Nothing to your Vehicle object's Engine property has no effect whatsoever on the Engine object. For a more specific example, does taking an engine out of a vehicle destroy the engine in real life?
As for your last question, normally you'd use a collection of some sort. There are countless examples of this in the Framework that you use all the time. A DataTable has a Rows property that is a DataRowCollection that contains DataRows. It also has a Columns property that is a DataColumnCollection that contains DataRows. In your case you should define an EngineCollection class that inherits System.Collections.ObjectModel.Collection(Of Engine). Your Vehicle class would then have a ReadOnly Engines property of that type. You can then Add or Remove items from that collection, just as you do with any collection.
-
Aug 31st, 2008, 01:50 PM
#3
Thread Starter
Lively Member
Re: Swapping Objects between Objects and Array of Objects
Last edited by kbrizs; Aug 31st, 2008 at 02:39 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|