-1

On my app a teacher can have several classes, and when I exclude a teacher's profile I have to, first, delete his classes. I'm trying to put each class_id of this teacher on an int array, to later delete all classes which the id is contained inside this array.

This is my code so far:

int x = 0;
int[] count = new int[x];

while (reader_SelectedClasses.Read())
{
    if(x != 0)
    {
        x++;
        count = new int[x];
    }
    count[x] = _class.Class_id = reader_SelectedClasses.GetInt16("class_id");
}

And this is what

reader_SelectedClasses.Read()

does:

select class_id from tbl_class where user_id = " + id + ";

And this is the return, when I try this on MySQL:

enter image description here

But it gives me back an IndexOutOfRangeException when I run the code on my DAO class. What am I missing? Already went here but didn't quite understand. Can someone please explain on few words and post and fixed code for this?

Community
  • 1
  • 1
  • The if condition will never executed and the value of x is never incremented. So you typically adding value to same index. – G K Feb 05 '17 at 18:38
  • After edit of your question, I withdraw my comments. – G K Feb 05 '17 at 18:39
  • you need to walk through your logic line by line and you will see exactly where the problem came from. You are trying to get the first index of an empty array. Also how is x going to be incremented? – Nkosi Feb 05 '17 at 18:41
  • @GK changed it all to zeros, still error. –  Feb 05 '17 at 18:43
  • 1
    @Pelicer the array is empty ie 0 items. You try to get item at index 0 which is the first index in a populated array but your array is empty so there is no index 0 thus index out of range. – Nkosi Feb 05 '17 at 18:48
  • In the edited code, the array is initialised with 0 size and then inside the while loop you have assigned a value which is something going wrong. If you don't know the size then probably switch to a list and use it. – G K Feb 05 '17 at 18:52

3 Answers3

1
  1. You need to learn how to use a debugger and step through your program.

  2. count = new int[x]; discards what was in count and creates a new array that contains zeroes. This array's indexes go from 0 to x - 1.

  3. count[x] = ... sets the array element at index x which according to the previous line is one past the end of the array.

You need to set count = new int[x] only once, at the beginning of your program, and set count[x] = ... only if x >= 0 and x < count.Length.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
0

You are getting IndexOutOfRange Exception because you are trying to access element from array which is out of range.

At first line you are setting x = 1. Hoping that controller enters while loop and as x is 1 it doesn't enter if loop and it executes next statement. But count[1] (x = 1) is not allowed as you have created array with only one element and that you can access with count[0]. (Array indexing starts from 0)

Pratik
  • 88
  • 1
  • 2
  • 11
0

You are trying to achieve a List behavior with an array.
Obviously, the IndexOutOfRangeException is because you initialize an empty array and then try to add values to it in a non-existing cell.

Try to convert to List<int>:

List<int> count = new List<int>();

while (reader_SelectedClasses.Read())
{
    int classId = _class.Class_id = reader_SelectedClasses.GetInt16("class_id");
    count.Add(classId);
}

If you really need an array out of it you can do:

int[] countArray = count.ToArray()
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
  • At the end I used Dour High Arch's answer, but this list worked as well and I will review my code. Maybe i'll use a List instead of a array. –  Feb 05 '17 at 18:52
  • Ofir's answer and mine can both be used. If you do not know the number of classes in `reader_SelectedClasses` until after you read it you should use a `List`. – Dour High Arch Feb 05 '17 at 19:53