Rob Jansen
Rob Jansen

Reputation: 59

Modify multiple entries in a generic list (without having to create a new variable for each one)

I wish to change the status of a list entry with a generic method.

My code is below. I figured out how to change one entry (for example, chicken number 2), but I want to change multiple chicken statuses at once, without assigning it to variables individually.

Ultimately, I want to change the status of a chicken from "Alive" to "Dead" with a method (that triggers when HP - not yet in the code - hits zero).

class Chicken_Test
{
    class Chicken
    {
        public Chicken(int inNumber, string status)
        {
            this.Number = inNumber;  // UNIQUE ID
            this.Status = status;
        }

        public int Number { get; set; }
        public string Status { get; set; }
    }

    public static void Main()
    {
        var chickens = new List<Chicken>();
        for (int i = 0; i < 100; i++)
        {
            chickens.Add(new Chicken(i,  "Alive"));
        }

        int testnummer = 2;

        var chickenTwo = chickens.SingleOrDefault(c => c.Number == testnummer);
        chickenTwo.Status = "Dead";

        var chickensAlive = chickens
                .Where(r => r.Status == "Alive")
                .GroupBy(c => c.Number)
                .Select(r => new
                {
                    Status = r.Key,
                    Count = r.Count()
                }).ToList();
    }
}

Upvotes: 0

Views: 109

Answers (4)

Derrick Moeller
Derrick Moeller

Reputation: 4950

Do you even need a method?

class Chicken
{
    public string Status
    {
        get { return _hp <= 0 ? "Dead" : "Alive"; }
    }
}

Alternatively.

class Chicken
{
    private int _hp;

    public int HP
    {
        get { return _hp; }
        set
        {
            if (_hp != value)
            {
                _hp = value;
                OnHPChanged();
            }
        }
    }

    public string Status { get; set; }

    private void OnHPChanged()
    {
        if (HP <= 0)
            Status = "Dead";
        else
            Status = "Alive";
    }
}

Upvotes: 2

James Curran
James Curran

Reputation: 103495

void KillChicken(Chicken chicken)
{
        chicken.Status = "Dead";
}

int[] testnummer = { 2,3,5,6};
foreach(var ch in chickens.Where(c=>testnummer.Contains(c.Number))
       KillChicken(ch);

Upvotes: 2

ElectricRouge
ElectricRouge

Reputation: 1279

int[] testnummer = { 2,3,5,6};

chickens = chickens.Select(x => new Chicken(x.Number,x.Status)
            {
             Status =  testnummer.Contains(x.Number)? "Dead" : x.Status     
            }).ToList(); 

OR

Insead of creating a new list you can update the current list.

chickens.Select(x => { x.Status = testnummer.Contains(x.Number) ? "Dead" : x.Status; return x;}).ToList()

Upvotes: 0

Tal Vinokurov
Tal Vinokurov

Reputation: 39

Did you try this?

chickens.ForEach(chicken => chicken.Status = "Dead")

By that way, I would advice using an enum instead of string in the Status variable so your code would look like this

class Chicken
{
    public enum ChickenStatus
    {
        Dead,
        Alive
    }

    public Chicken(int inNumber, ChickenStatus status)
    {
        this.Number = inNumber;
        this.Status = status;
    }

    public int Number { get; set; }
    public ChickenStatus Status { get; set; }
}

And maybe also replace class with struct, it seems much more appropriate.

Upvotes: 0

Related Questions