Parsing delimited data into class
I've created several class objects that take a string array in the constructor. Each property in the class is 'linked' to a ordinal position in the array. Below is an example of how I am populating the class, but it seems really clunky. I'm wondering if you guys or gals have thoughts on how to clean it up.
Code:
public NM1_SubmitterName_1000A(string[] elements)
{
if (elements.Length > 1 && elements[1] != string.Empty)
{
this.NM101__EntityIdentifierCode = elements[1];
}
if (elements.Length > 2 && elements[2] != string.Empty)
{
this.NM102__EntityTypeQualifier = elements[2];
}
if (elements.Length > 3 && elements[3] != string.Empty)
{
this.NM103__SubmitterLastOrOrganizationName = elements[3];
}
if (elements.Length > 4 && elements[4] != string.Empty)
{
this.NM104__SubmitterFirstName = elements[4];
}
if (elements.Length > 5 && elements[5] != string.Empty)
{
this.NM105__SubmitterMiddleNameOrInitial = elements[5];
}
if (elements.Length > 8 && elements[8] != string.Empty)
{
this.NM108__IdentificationCodeQualifier = elements[8];
}
if (elements.Length > 9 && elements[9] != string.Empty)
{
this.NM109__SubmitterIdentifier = elements[9];
}
}
Re: Parsing delimited data into class
Code:
for (var i = 1; i <= 9; i++)
{
if (elements.Length > i && elements[i] != string.Empty)
{
// Use Reflection to get a PropertyInfo for the appropriate property based on i.
}
}