|
-
April 6th, 2012, 12:12 AM
#1
Pointer Workaround
I'm coming to C# with some experience in C++.
Suppose I have the following
class Area
{
Foo someObject;
}
class Ship
{
Area currentLocation;
}
static void Main()
{
Ship theShip = new Ship();
Area theArea = new Area();
theShip.currentLocation = theArea;
//Now I have some function, to which the ship is passed
doSomething(theShip);
}
//Function definition
void doSomething(Ship ship)
{
Foo newObject;
ship.currentLocation.someObject = newObject;
}
Is there a convenient way to link the currentLocation object in the ship class to the instance of theArea defined in main? In C++ I would have just stored currentLocation as a pointer to do this. I need to be able to pass the theShip object to a function and have that function modify the properties of the Area associated with the ship.
My current implementation uses a list of Area objects which each have an ID, and I store the ID in the currentLocation variable and use a Dictionary to lookup the index of the Area object so that I can go back and access the list. This all seems terribly inefficient.
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
|