#### SOLUTION ####
Ended up doing this which is a less efficient but more readable version of David's code. As a result, I have memoralized him in the actual software. Actual code below:
//This portion, while functional, earned a "meh" from David N. in #GBC due to possibly unneeded IF checks
newCRTDetail.MaximumCopayTier = (data.Length > 17) ? Formatting.ToNullableInt(data[17]) : null;
newCRTDetail.CopayTier = (data.Length > 16) ? Formatting.ToNullableInt(data[16]) : null;
newCRTDetail.DaysSupplyPerCopay = (data.Length > 15) ? Formatting.ToNullableInt(data[15]) : null;
newCRTDetail.MaximumCopay = (data.Length > 14) ? Formatting.ToNullableDouble(data[14]) : null;
newCRTDetail.MinimumCopay = (data.Length > 13) ? Formatting.ToNullableDouble(data[13]) : null;
newCRTDetail.FirstCopayTerm = (data.Length > 12) ? data[12] : String.Empty;
newCRTDetail.PercentCopayRate = (data.Length > 11) ? Formatting.ToNullableDouble(data[11]) : null;
newCRTDetail.FlatCopayAmount = (data.Length > 10) ? Formatting.ToNullableDouble(data[10]) : null;
#### ORIGINAL POST ####
So - I have an issue and I'm stumped. I hate using GOTO statements and can't believe C# actually requires them sometimes.
Problem: I have a string array named data. It can have up to 18 elements [0] - [17] and the elements 0 - 9 will always be there. Each element, if it exists, gets assigned to a different property. This is one way to do it:
newCRTDetail.PropA = data[2];
newCRTDetail.PropB = data[3];
newCRTDetail.PropC = data[5];
newCRTDetail.PropD = data[6];
newCRTDetail.PropE = data[7];
newCRTDetail.PropF = data[8];
newCRTDetail.PropG = data[9];
switch (data.Length) //not all values are required and optional values at the end may be omitted
{
case 17:
newCRTDetail.PropO = Formatting.ToNullableInt(data[17]);
goto case 16;
case 16:
newCRTDetail.PropN = Formatting.ToNullableInt(data[16]);
goto case 15;
case 15:
newCRTDetail.PropM = Formatting.ToNullableInt(data[15]);
goto case 14;
case 14:
newCRTDetail.PropL = Formatting.ToNullableDouble(data[14]);
goto case 13;
case 13:
newCRTDetail.PropK = Formatting.ToNullableDouble(data[13]);
goto case 12;
case 12:
newCRTDetail.PropJ = data[12];
goto case 11;
case 11:
newCRTDetail.PropI = Formatting.ToNullableDouble(data[11]);
goto case 10;
case 10:
newCRTDetail.PropH = Formatting.ToNullableDouble(data[10]);
break;
}
Another way would be:
if (data.length == 17)
newCRTDetail.PropO = Formatting.ToNullableInt(data[17]);
if (data.length >= 16)
newCRTDetail.PropN = Formatting.ToNullableInt(data[16]);
...
if (data.length >= 10)
newCRTDetail.PropH = Formatting.ToNullableDouble(data[10]);
Now, I may just be having a brain fart here, but is there a better way than either of those? A switch statement that allowed fall-through would be much shorter and easier to read (IMHO).
Update - another option:
if (data.length >= 10){
newCRTDetail.PropH = Formatting.ToNullableDouble(data[10]);
if (data.length >= 11){
newCRTDetail.PropI = Formatting.ToNullableDouble(data[11]);
if (data.length >= 12){
newCRTDetail.PropJ = data[12];
}
}
}
Update 2 - Thanks David for helping me get past a brain fart.
Update 3 - possibly even better (a bit more overhead since every 'if' is checked but definitely easier to read)
newCRTDetail.MaximumCopayTier = (data.Length > 17) ? Formatting.ToNullableInt(data[17]) : null;
newCRTDetail.CopayTier = (data.Length > 16) ? Formatting.ToNullableInt(data[16]) : null;
newCRTDetail.DaysSupplyPerCopay = (data.Length > 15) ? Formatting.ToNullableInt(data[15]) : null;
newCRTDetail.MaximumCopay = (data.Length > 14) ? Formatting.ToNullableDouble(data[14]) : null;
newCRTDetail.MinimumCopay = (data.Length > 13) ? Formatting.ToNullableDouble(data[13]) : null;
newCRTDetail.FirstCopayTerm = (data.Length > 12) ? data[12] : String.Empty;
newCRTDetail.PercentCopayRate = (data.Length > 11) ? Formatting.ToNullableDouble(data[11]) : null;
newCRTDetail.FlatCopayAmount = (data.Length > 10) ? Formatting.ToNullableDouble(data[10]) : null;