0

So I have some code which stores some data in an array. When new data comes in it is put in a new array item (The totalnumber of array items) then the total number of array items is added too ready for the next bit of data. But when I try to add data into the array, be it array[0] or whatever it throws array index out of bounds?

Declaration:

string[] TabMessages = { };
int TotalTabs = 0;

Using it:

DevComponents.DotNetBar.TabItem Tab = TabStrip.CreateTab(TabName);
Tab.Tooltip = id + "|" + TabIndex;
TabMessages[TotalTabs] = "";//index out of bounds of array
TabStrip.SelectedTab = Tab;
TotalTabs++;

Any help, this is really annoying me because it's throwing the error about the index being out of bounds of the array when I'm trying to create a new entry to the array...

1
  • Arrays need to be allocated with the size required. If you want a dynamic array try List Commented Feb 18, 2013 at 4:41

4 Answers 4

2

Arrays are a static length. You have defined an array of 0 length, then tried to access an element in the array that does not exist. Either you have to create a large enough array to hold all of the values you intend to use, or use a non-static collection like List<string> instead of a static-sized one like string[].

List<string> TabMessages = new List<string>();
TabMessages.Add("");

If you want something you can access by index, but don't want to supply all possible values, use a dictionary:

Dictionary<int, string> TabMessages = new Dictionary<int, string>();
TabMessages[TotalTabs] = "";
Sign up to request clarification or add additional context in comments.

Comments

1

Arrays in C# are not dynamic - they are fixed size. Try using something list a List<string> and using the Add method to insert a new entry into it.

TabMessages is an array of 0 elements (that's how you declared it). As such, you won't be able to add (or set) any element on it - you'll get an index out of bounds exception every time.

1 Comment

Worked perfectly. Thanks, Also thanks to everyone else that replied for helping :)
1

This code:

string[] TabMessages = { };

is equivalent to:

string[] TabMessages = new string[0];

it means you created array which size 0. That is why you got this kind of exception. So you can use List<string> instead with dynamic size:

var TabMessages = new List<string>();

Then you can add the first item:

TabMessages.Add(string.Empty);

Or, create string array with fixed size depending on your business rule:

string[] TabMessages = new string[5];  // create string array with 5 elements

Comments

0

Array's are defined as having a fixed size. Declaring string[] TabMessages = {} is the same as string[0] Tabmessages;

You can resize an array (but I think it is a moderately expensive process) - see http://www.dotnetperls.com/array-resize

Alternatively (and preferably), try using some type of List construct

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.