0

My Main problem is : if i add "N" group to company and check it in the last , i see all of "Man"s arrange into all of groups like together ?

i this my problem is in the definition of class or references .

This is my code :

    public class Man
    {
        public int Code { get; set; }
        public string Name { get; set; }
        public int Priority { get; set; }
        public int Stoptime { get; set; }
        public Boolean Lunch { get; set; }
        public DateTime Arrival { get; set; }
        public DateTime Departure { get; set; }
        public int LunchTime { get; set; }
    }

    public class Group
    {
        public List<Man> People { get; set; }
        public double Speed { get; set; }
        public double Rate { get; set; }
        public double Surcharge { get; set; }
        public double TotalRate { get; set; }
    }
    public class Company
    {
        public List<Group> Groups { get; set; }
        public Group BestGroup { get; set; }
        public double Rate { get; set; }
        public double Surcharge { get; set; }
        public double FullRate { get; set; }
    }

    private List<Man> ShufflePosts(List<Man> ShufflePeoples)
    {
        List<Man> Temp = ShufflePeoples;
        List<Man> Commixed = new List<Man>();
        Random rand = new Random();
        do
        {
            int shf = rand.Next(0, Temp.Count);
            Commixed.Add(Temp[shf]);
            Temp.RemoveAt(shf);
        } while (Temp.Count > 1);

        Commixed.Add(Temp[0]);
        return Commixed;
    }


    public void CAStart(List<Man> Peoples)
    {

        var _Race = new Company();
        _Race.Groups = new List<Group>();
        for (int i = 0; i < 5; i++)
        {
            var Gr = new Group();
            Gr.People = ShufflePosts(Peoples);
            _Race.Groups.Add(Gr);
        }
    }

In the code Commixed.Add(Temp[0]); VS show me error index out of range. I check the variable and see below data:

ShufflePeoples.Count = 0, Temp.Count = 0, Commixed.Count = 1

Why this happens ? Where is my problem ?

4 Answers4

2

Why you get the error:

Your do while loop runs until Temp.Count > 1 isn't true - which will happen when you removed all items from it with line Temp.RemoveAt(shf);.

Then you try accessing Temp[0] (the first item) but temp is empty and you get an index out of range error.

Try to change your loop's condition and avoid accessing a specific position in the collection without checking that that position exists. Or better still use a simple while instead and then you won't need to specially address the last item in Temp


A nice solution for shuffling:

var suffled = ShufflePeoples.OrderBy(item => Guid.NewGuid());
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • Why not add items to "Commixed" and Why removed item into ShufflePeoples –  Jul 04 '16 at 08:17
  • I don't understand what you mean. Hope I answer right. you don't get it when you add the items to the `Commixed` because you first chose a position that is for sure in the rand. A nicer way will just be to you a simple `While` loop instead. That way you don't need to address that last item in the collection afterwards. – Gilad Green Jul 04 '16 at 08:22
0

You remove all Temp items in do-while loop, so when you try to access Temp[0] after loop, it will give you index out of range.

currarpickt
  • 2,290
  • 4
  • 24
  • 39
  • Why not add items to "Commixed" and Why removed item into ShufflePeoples –  Jul 04 '16 at 08:16
0

Change your loop to avoid index out of range error:

  while (Temp.Count > 0)
    {
        int shf = rand.Next(0, Temp.Count);
        Commixed.Add(Temp[shf]);
        Temp.RemoveAt(shf);
    };

When you remove item from Temp, you also remove it from ShufflePeoples because you refer Temp = ShufflePeoples, to avoid it, just make new list then copy items from ShufflePeoples to Temp.

Nam Tran
  • 643
  • 4
  • 14
0

For the first time, ShufflePosts method will remove all the contents of the ShufflePeoples. Therefore the second time you run ShufflePosts method, ShufflePeoples or Temp' is basically empty, which means if you try to accessTemp[0]`, it will give you index out of range Exception.

My 2 cents:

  1. Avoid assigning Temp = ShufflePeoples, instead do Copy Constructor Temp = new List<Man>(ShufflePeoples), to make sure that you do not adjust the parameter accidentally
  2. Always check your initial condition of your parameter argument.
kurakura88
  • 2,185
  • 2
  • 12
  • 18