Reputation: 761
I would appreciate some help refactoring my code. If I pass an instance of an object to another object instance is there any way to reinstantiate that instance without losing the reference?
Here is a simplified version of the problem I have:
public class Program
{
static void Main(string[] args)
{
Animal myPet = new Cat();
House myHouse = new House(myPet);
House petDayCare = new House(myPet);
Console.WriteLine(String.Format("My pet has {0} flea(s)", myPet.Fleas.ToString()));
myHouse.AddFlea();
Console.WriteLine(String.Format("My pet has {0} flea(s)", myPet.Fleas.ToString()));
petDayCare.AddFlea();
Console.WriteLine(String.Format("My pet has {0} flea(s)", myPet.Fleas.ToString()));
myHouse.GetNewPet();
Console.WriteLine(String.Format("My pet has {0} flea(s)", myPet.Fleas.ToString()));
Console.ReadLine();
}
}
public class House
{
Animal _currentPet;
public House(Animal currentPet) {
_currentPet = currentPet;
}
public Animal CurrentPet {
get { return _currentPet; }
set { _currentPet = value; }
}
public void AddFlea() {
_currentPet.Fleas += 1;
}
public void GetNewPet() {
Animal rover = new Dog();
rover.Fleas = 100;
_currentPet = rover;
}
}
public abstract class Animal {
int _fleas;
public int Fleas {
get { return _fleas; }
set { _fleas = value; }
}
public abstract string GetSound();
}
public class Cat : Animal
{
public override string GetSound() {
return "Meow";
}
}
public class Dog : Animal
{
public override string GetSound()
{
return "Woof";
}
}
Can myPet.Fleas = 100 in Program after calling GetNewPet()?
If I want to change the underlying type of object instance can I do that? If not, does anyone have any refactoring suggestions?
I will try and describe the details of the actual program structure.
I am creating a WPF Wizard.
That Wizard has steps.
When I start the Wizard an instance of the WizardViewModel class is created.
WizardViewModel class has an instance of ObjectX (an object whoes properties I want to modify and then return the object when the wizard is complete).
Each step in the wizard is an instance of the WizardPageViewModelBase class which is passed an instance of ObjectX in the constructor.
WizardViewModel has a list of WizardPageViewModelBase objects (one object instance for each wizard step).
So as long as I am only changing the properties of the ObjectX instance in each wizard step (WizardPageViewModelBase object instance) then the object that is returned by WizardViewModel works fine.
However in step 1 of the wizard I want to change the underlying type of ObjectX depending on what the user selects from a listbox. If I change the instance (as I do in GetNewPet() above) the reference to the ObjectX instance in WizardViewModel is lost.
Apologies if this makes no sense, I tried my best to structure the question in a way that can be answered...
Upvotes: 2
Views: 205
Reputation: 117064
The description of your problem seems reasonably clear, but I don't see how your sample code directly applies.
If you're creating a wizard where you want to change the object that you're applying the steps of the wizard on then you shouldn't actually create the instance until the last moment. The issue becomes how to apply the wizard steps if you don't create the instance until later. But that's easy.
What you need to do is to create a List<Action<Animal>> actions
and then when you create the Animal
instance you just run through the list and apply the action to the new instance. If you change the instance you just re-run the list.
var animal = new Cat();
foreach (var action in actions)
{
action(animal);
}
Does this help?
Upvotes: 0
Reputation: 6450
With the following code, where _fleas is made static, I can get "My pet has 100 flea(s)" but am not sure if that 's what you want:
public abstract class Animal
{
static int _fleas;
public int Fleas
{
get { return _fleas; }
set { _fleas = value; }
}
public abstract string GetSound();
}
Upvotes: 0
Reputation: 25513
You're never using the House's pet.
Even though you call myHouse.GetNewPet() which effectively replaces the CurrentPet of myHouse, it doesn't matter because you're never using Current Pet.
Your main program only uses myPet, which is always the Cat you created at the beginning.
So, in effect, your problem isn't that you're losing a reference to an Animal, it's that you never gave up the reference to begin with.
I imagine what you're intending to do is something to the effect of:
Console.WriteLine(String.Format("My pet has {0} flea(s)",
myHouse.CurrentPet.Fleas.ToString()));
Upvotes: 1