I’m having a very odd problem in C# and I’m not sure what is the cause. Take a look at the following code snippet:
foreach(string bed in bayBeds)
{
string[] bedProperties = bed.Split(new char[] { '^' });
if (bedProperties.Length > 0)
{
string genderCode = bedProperties[1];
if (genderCode == "M")
{
bedCount = bedCount + bayBeds.Count;
break;
}
}
}
In this example, the string array bedProperties is tested to see if its length is greater than 0, and if so, element 1 is retrieved. The problem is that this code always generates an out of bounds exception. I can modify to return bedProperties.Length and it will give me a value such as 3 (which is in fact the number of properties in this object), yet any attempt to get an array element by index (such as bedProperties[1], bedProperties[0], etc.) will always give me an out of bounds exception. Always. I can’t understand why this would be.
Please understand I’m something of a c# hack, so if I’ve made some ridiculously stupid mistake, please don’t be overly harsh.
Thanks – I appreciate all help.
EDIT: I found the issue based on much of the assistance below.
For clarity, this is the entire function:
public int returnMaleBedTotal(string bedCollection) {
// determine total number of male beds for a bay
int bedCount = 0;
if (bedCollection.Length > 0) {
List<string> theBays = new List<string>(bedCollection.Split(new char[] { '@' }));
// at this point we have the bays, so iterate them and extract the beds in the bays
foreach (string bayBedCollection in theBays) {
List<string> bayBeds = new List<string>(bayBedCollection.Split(new char[] { '|' }));
// now we have the beds in the bay, so extract the bed properties and determine if the bed is male
foreach(string bed in bayBeds) {
string[] bedProperties = bed.Split(new char[] { '^' });
if (bedProperties.Length > 1) {
string genderCode = bedProperties[1];
string bedStatus = bedProperties[2];
if (genderCode == "M") {
bedCount = bedCount + bayBeds.Count;
break;
}
}
}
}
}
return bedCount;
}
This takes a collection in the form of a big string that looks like this:
100000^^3|100002^^1|100003^^3|100004^F^2|100005^^2@100006^^1|100007^^2|100008^M^2|100009^^1|100010^^3@100011^M^2|100012^M^2|100013^M^1|100014^M^2|100015^M^1@100016^F^1|100017^^1|100018^F^1|100019^^1|100020^^1
It then chops that up into units that look like this:
100000^^3|100002^^1|100003^^3|100004^F^2|100005^^2
Which it further parses to units like this:
100005^^2 or 100004^F^2
On occasion, during these iterations one of these units would come back malformed and would have a length of 1, so the attempt to get the an indice > 0 would fail.
By the way, this is an extension method inside a transform, and that’s the reason for taking the initial collection as a big string.
Thanks to all who helped – sorry I can’t choose more than one correct answer.
Should really be:
Because any string, when split, will return itself in a single element array. If any splitting actually took place, there would be two or more elements in the array.