Results 1 to 2 of 2

Thread: Parsing delimited data into class

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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];
                }
            }
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width