Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static int GetRoom_SLot_ID(List<int> IndArray, int i)
   {
           int ind = (int)IndArray[i];
           return ind;
   }

 public static List<int> RoomID(int session_id)
    {
        List<int> Room_IDList = new List<int>();

        SqlConnection connecetion = ConnectingDatabase.GetConnection();
        string command = "SELECT Room_ID FROM Room ";
        SqlCommand commn = new SqlCommand(command, connecetion);

        SqlDataReader reader = commn.ExecuteReader();

        while (reader.Read())
        {
            Room_IDList.Add(reader.GetInt32(0));
        }
                connecetion.Close();
               return Room_IDList;
              //this is the method that it return the list >>>and the index is acounter
    }
gettingBlockIDs = GettingValuesIArray.AssignmenBlock_IDs();///getting all IDs in list

/////i call it like that
 block_indx = GettingValuesIArray.GetRoom_SLot_ID(gettingBlockIDs, counter);//this to get value ///inside the list according to the index 

///this is the class with the counter
             counter =0;

        for (int i = 1; i < slots; i++)
        {
            //  studen_number;///i need to check how to get student number
            for (int j = 1; j < rooms; j++)
            {
                ///gettin Assignments IDS
                gettingBlockIDs = GettingValuesIArray.AssignmenBlock_IDs();///getting all IDs in list
                block_indx = GettingValuesIArray.GetRoom_SLot_ID(gettingBlockIDs, counter);
                gettingRooms = GettingValuesIArray.RoomID(1);///getting all rooms IDs in one list
                gettingRoom_ID = GettingValuesIArray.GetRoom_SLot_ID(gettingRooms, i);

                block = new Chromsome(block_indx,gettingRoom_ID);
                room_capacity = block.getRoomCapacity();///getting Room Capacity
                group_id=block.getGroup_ID();

                block2 = new Chromsome(group_id);
                student_Capacity = block2.getGroupCapacity();

                        if (population[i, j] == 0 && student_Capacity <= room_capacity )
                    {

                        population[i, j] = block_indx;
                        counter++;
                        return population; ///i returned the (partial) population in each loop to check for over lapping 

                    }
                    else
                        continue;
                }

            }

            return population;///now i return the final population
        }
    }

tell me out of range
Posted
Updated 3-May-13 7:37am
v8
Comments
CHill60 3-May-13 10:16am    
Use the Improve question link to give details of what is in IndArray and what you are passing in for i.
mahmoud.abdallah 3-May-13 10:28am    
i did
Pheonyx 3-May-13 10:33am    
Show us the code that calls "GetRoom_SLot_ID"
mahmoud.abdallah 3-May-13 10:37am    
done
Pheonyx 3-May-13 10:44am    
When you step through the code and get to the line

block_indx = GettingValuesIArray.GetRoom_SLot_ID(gettingBlockIDs, counter);

What is the count value on "gettingBlockIDs" and what is the value of "counter" ?

That's because your list simply does not contain an element at the given index. First check before the index access if the questioned element exists in the first place.
C#
public static int GetRoom_SLot_ID(List<int> IndArray, int i)
{
    if(IndArray.ElementAtOrDefault(i) != 0)
           int ind = (int)IndArray[i];
           return ind;
    }
    return -1;
}
 
Share this answer
 
v4
Comments
mahmoud.abdallah 3-May-13 10:27am    
it did not work
max_nowak 3-May-13 11:05am    
Well, I provided you a solution for the problem you asked for. The reason why your list does not contain the index is a whole different story.
First thing, Arrays start at 0, not 1 so change
C#
for (int i = 1; i < slots; i++)

to
for (int i = 0; i < slots; i++)


Do the same for both loops.

Secondly, Can you please answer My question regarding count value on the array, and answer CHill60's question regarding the contents of the array!
 
Share this answer
 
v2
You haven't posted sufficient code for me to be able to reproduce your code to a compilable stage (and I'm not prepared to wade through 30+ errors on your behalf!)

However you appear to be passing into the function the students that can fit in a room available rather than the number of rooms perhaps? You have no checks on counter++

So the answer is that there is nothing *wrong* with your function GetRoom_SLot_ID (although you could improve it as suggested by max_nowak)

The loops that you are going through (which should be fixed as per Pheonyx' solution above) need some attention to prevent counter exceeding the count of items in the gettingBlockIDs list.

I can't really guide you much further as it's not that clear what you are trying to do. You'll need to step through your code to determine at which point counter exceeds gettingBlockIDs.Count. That should give you enough insight to work out what's wrong with your logic.
 
Share this answer
 
Comments
mahmoud.abdallah 3-May-13 23:30pm    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900