2019-08-10 11:55:02

hello!
So, I have my player class:
        public int HitPoints { get; set; }
        public string PlayerName { get; set; }
        public bool IsDead { get; set; }
        public int PlayerAttack { get; set; }
        public int PlayerDefense { get; set; }
        public int PlayerSleep { get; set; }
    }
}
Then, I have my PlayerSkill class:
        public int SkillID { get; set; }
    public string SkillName { get; set; }
        public int SkillDamage { get; set; }
        public int SkillSuccessRate { get; set; }
    }
}

How can I do that my player will have a list of skills as one of the constructor's passable arguments?

Raduvay se, raduvay
Raduvay se domaki ne
Kolko liste po gorach
Tolko zdrave na taz kyshcha

2019-08-10 14:50:12

Hi there,
first, in the player class, if you want to expose player's skills as property:

private List<PlayerSkill> skills;
public ReadOnlyCollection<PlayerSkill> Skills {get; private set;}

If you used the List class for this purpose, while it wouldn't be possible to change its instance from outside, there would be still possibility to call Add on it, what you normally don't want until there is a good reason for it. That's why ReadOnlyCollection is used. It's a wrapper around List, skills with small s in this case, which you can see above it. It presents its content like a normal List, with only exception that there are no modifying methods, so outside caller can't mess it up.
Whenever you want to add skills to it, you just add items to underlying List and it will have immediate effect, because ReadOnlyCollection is just a wrapper around it as I said.

Then in Player's constructor (I'm omitting other parameters):

public Player(List<PlayerSkill> skills)
{
this.skills=skills;
Skills=new ReadOnlyCollection<PlayerSkill>(this.skills);
}

Although now there is still at least one reference to internal class field outside of it, because List is a reference datatype. If you want to completely prevent this, make a new copy like this:

public Player(List<PlayerSkill skills)
{
this.skills=new List<PlayerSkill>();
this.skills.AddRange(skills);
Skills=new ReadOnlyCollection<PlayerSkill>(this.skills);
}

Just for completeness, here is a preview of an AddSkill method

public void AddSkill(PlayerSkill skill)
{
skills.Add(skill);
}

Yeah, and don't forget to include System.Collections.ObjectModel namespace, so you have access to the ReadOnlyCollection.

You can find further reference at:
https://docs.microsoft.com/en-us/dotnet … mework-4.8

Best regards

Rastislav

Thumbs up

2019-08-10 20:33:48

Thanks Rastislav for your explanation

Raduvay se, raduvay
Raduvay se domaki ne
Kolko liste po gorach
Tolko zdrave na taz kyshcha