Don't know if the title of the post describes what I want and trying to achieve.
But I try to explain it below.
But I try to explain it below.
In the Toy class there's a LowerQuality method from which I want to remove the current toy for an animal if the quality is 0:
abstract class Toy
{
public int Quality { get; set; } = 10;
public virtual int LowerQuality(int inputQuality)
{
int calcQuality = Quality - inputQuality;
Quality = calcQuality;
Console.WriteLine(" Play has finished!");
if (Quality <= 0)
{
Quality = 0;
Console.WriteLine($" Quality of the {Name} has decreased with " + inputQuality + " and is now waste and removed from toys list");
Petowner.Pets[???].Toys.Remove(this); // 1. How can I tell what index of animal that actually call this method?
Petowner.Pets[0].Toys.Remove(this); // 2. This will work since the animal is specified by a fixed index which needs to be dynamic however
}
else
{
Console.WriteLine($" Quality of the {Name} has decreased with " + inputQuality + " and is now " + Quality);
}
return Quality;
}
}
The object that's calling the method LowerQuality() in the Toy object:
class Cat : Animal
{
public override void Interact(Toy toy)
{
if (toy is Bone bone)
quality = 1;
else if (toy is Ball ball)
quality = 4;
toy.LowerQuality(quality); // In this case the cat calls the LowerQuality() method
// Having the code like this i.e. in the cat object makes it possible to remove the current toy item. But having it like this also forces me to have duplicated code in every animal object...
//if (qualityCheck == 0)
//{
// Toys.Remove(toy);
//}
}
}
Last edited: