-5

I have a string called SelectedItem that i want to split into 5 strings to then use in a textbox. The code is below:

string[] Details = SelectedItem.Split(',');

                string EmployeeID = Details[0];
                string EmployeeName = Details[1];
                string EmployeePay = Details[3];
                string EmployeeHours = Details[4];
                decimal TempWage = int.Parse(Details[3]) * int.Parse(Details[4]);
                string EmployeeWage = TempWage.ToString();

                TbxWage.Text = "Employee " + EmployeeID + "Wage Details " + Environment.NewLine + "pay rate is " + EmployeePay + Environment.NewLine + "Working Hours is " + EmployeeHours + Environment.NewLine + "Wage is " + EmployeeWage;

This code should run when I click a certain button but instead I get "Index was outside the bounds of the array" and I don't understand what it means.

Leyght
  • 1
  • It means that `SelectedItem.Split(',');` Does not return 5 items. Therefore, when you access one of the items, it doesn't exist. The index provided inside of `Details[x]` Does not exists because `x` is outside of the bounds of `Details`. What information is in `SelectedItem`? – S. Walker Oct 18 '17 at 01:29
  • Please show the contents of `SelectedItem` in order to point out _exectly_ why this code fails. – John Alexiou Oct 18 '17 at 01:30
  • I noticed `Details[2]` is never used. – John Alexiou Oct 18 '17 at 01:31
  • This means the same thing it means in every other one of the hundreds of questions here with the same error message. it means you went outside the bounds of the array, meaning either you don't have as many items in the array as you thought or you used the wrong indexes (e.g., starting with 1 instead of zero and therefore going beyond the end). VS has a debugger that works to step through code to figure out problems like this one; you should learn to use it. – Ken White Oct 18 '17 at 01:32

1 Answers1

0

This means your string you were spitting didn't split into the number of parts you were expecting.

you can check before using

if(Details.Length == 5) 
{
   // do stuff
}
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156