I'm getting an index out of bounds error when I request the last element in the list for a C# Windows Form Application.
Does anyone know what might be causing this because it literally makes no sense whatsoever. It's as if the computer is miscalculating. The size is supposedly 17, but everything above index 5 gives an error. Once logic goes out the window, I've got nothing left. Perhaps there's something weird going on behind the scenes that someone else may have encountered before?
List<string> words = new List<string>();
List<string> words = new List<string>();
string word = "";
int idx = 0;
while (idx < notes.Length)
{
word += notes[idx];
if (notes[idx] == ' ')
{
words.Add(word);
word = "";
}
++idx;
}
string notes1 = "";
string notes2 = "";
string notes3 = "";
int one_third = words.Count / 3;
int two_thirds = (words.Count / 3) * 2;
int k;
for (k = 0; k < one_third; k++)
notes1 += words.ElementAt(k) + ' ';
for (k = one_third; k < two_thirds; k++)
notes2 += words[k] + ' ';
for (k = two_thirds; k < words.Count; k++)
notes3 += words[k] + ' ';
notesLabel1.Text = notes1;
notesLabel2.Text = notes2;
notesLabel3.Text = notes3;
++++++++++++++++++++++++++++++++++++++++++++++
FOUND THE PROBLEM!!!!!!
Basically, I overworked myself yesterday so my brain was fried by the end of the day and I was irritable. The function code works just fine unless, like many have said, the notes string is empty. I knew that the notes string wasn't empty because it posts just fine without the +1 portion in the for-loop. BUT I FORGOT ABOUT ONE THING. The first 'item' that gets posted to the form is the first 'item' in the array I have in my program. Even though the item notes in question do indeed have 17 words, it's the second item in the list. The first item in the list gets posted when the application loads and I simply scroll over to the item with the 17 word notes. The first item that's posted doesn't have notes so the first time that function is called, the argument is an empty string. OOPS! feels dumb
Thanks, everyone!! I appreciate you taking the time to help fix my nonsense here. haha