How do I implement ToString() for my Player object?

If I understand the question correctly, you simply want to change your ToString() method to look like this:

public override string ToString()
{
    return string.Format("Din totala summa är {0}{1}", Name, GetScore());
}

Now, that said, you probably have a number of other problems. It seems highly unlikely that the Arrows class should inherit the Player class. Inheritance implies an “is a” relationship. I.e. your code is saying that a group of arrows is a player. I suppose in some specific game that might be true, but it’s a very unusual design and doesn’t seem like one you really meant.

On the other hand, your Player class already has a list of Arrows objects (reinforcing my suspicion that your Arrows class shouldn’t inherit Player…if it were true that Arrows is a Player, why would Player have a list of them?).

Unfortunately, there’s not enough information to know what you really meant to do here. Possibly you should just change the ToString() method in the Player class instead, e.g. something like:

public override string ToString()
{
    return string.Format("Din totala summa är {0}{1}", Name, trow.Count);
}

Or maybe something like this:

public override string ToString()
{
    return string.Format("Din totala summa är {0}{1}", Name, trow.Sum(a => a.GetScore()));
}

But I can’t really say for sure.

Leave a Comment