I am trying to check if the position of an array exists or not.
I am trying to output positions 1, 2, 3, 4 and 5 string values from a list. Where the list is less than 5, it needs to display a '-' as the string value.
For example, a list of 3 should display: Value, Value, Value, -, -
I cannot however work out how to check this, and I keep getting index was out of range errors.
I have tried:
if (String.IsNullOrEmpty(formGuideCount[3]))
{
game4 = formGuideCount[3];
}
else
{
game4 = "-";
}
Can anyone tell me what I should be using to check if that position does not exist?
Thanks
You can use the Array.Length of array to validate the index location exists.
if(formGuideCount.Length > 3)
{
game4 = formGuideCount[3];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With