CSharpDev4Evr
CSharpDev4Evr

Reputation: 545

c# "Index was outside the bounds of the array."

Trying to solve this problem I am having with using array information in my request.

Here is the code I wrote:

public int[] custid = new int[] {};

_request2.CustID = custid[_response.Customers[3].CustID];

Now after debugging the program and walking through each part of the above line, everything is correct, but when I try and run the program it gives me the error "Index was outside the bounds of the array."

Any ideas on what I am doing wrong?

Upvotes: 0

Views: 262

Answers (1)

David Hope
David Hope

Reputation: 2256

You allocated an empty array. Then you tried to access an element of your empty array.

You need to have elements in the array like:

public int[] custid = new int[10];

or

public int[] custid = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

If you are looking for something more flexible (like being able to add and remove elements), I suggest a list.

Upvotes: 2

Related Questions