Best way to add all public records to a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tig2810
    New Member
    • Aug 2008
    • 7

    #1

    Best way to add all public records to a list

    Hi

    I was wondering if someone could let me know the best/correct way of doing this. I have a shortened example of code below:

    In the beginning of the class I have a List of string containing all records. I have added each encapsulated field to the list in the set property and I need to maintain the order in the list. Is this way best way to do this? Or should I be doing this another cleaner way. I guess I’m asking for best coding techniques.

    Thanks for any help

    Code:
    Class myclass()
    {
    
            public List<string> allRecords = new List<string>();
            
            // Basically i am appending whatever is set to the end of the string with all these
    
            string _recordStart = "HEADERSTART"; 
            public string RecordStart
            {
                get { return _ recordStart; }
                set 
                { 
                    _ recordStart += value;
                    allRecords.Add(_recordStart);
                }
            }
    
            private string _example= "EXAMPLE_STRING"; 
            public string Example
            {
                get { return _ example; }
                set 
                {
                    _example+= value; 
                    allRecords.Add(_example); 
                }
            }
    
    // Many more encapsulated fields
    
            string _recordEnd= "HEADEREND"; 
            public string HdrRecordEnd
            {
                get { return _ recordEnd; }
                set 
                { 
                    _ recordEnd += value;
                    allRecords.Add(_recordEnd);
                }
            }
    }
    Last edited by tlhintoq; May 30 '09, 07:04 PM. Reason: [CODE] ... your code here ... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by tig2810
    Code:
    Class myclass()
    {
    
            public List<string> allRecords = new List<string>();
            
            // Basically i am appending whatever is set to the end of the string with all these
    
            string _recordStart = "HEADERSTART"; 
            public string RecordStart
            {
                get { return _ recordStart; }
                set 
                { 
                    _ recordStart += value;
                    allRecords.Add(_recordStart);
                }
            }
    
            private string _example= "EXAMPLE_STRING"; 
            public string Example
            {
                get { return _ example; }
                set 
                {
                    _example+= value; 
                    allRecords.Add(_example); 
                }
            }
    
    // Many more encapsulated fields
    
            string _recordEnd= "HEADEREND"; 
            public string HdrRecordEnd
            {
                get { return _ recordEnd; }
                set 
                { 
                    _ recordEnd += value;
                    allRecords.Add(_recordEnd);
                }
            }
    }
    Lines 14 and 38. That space between the underscore and recordEnd is going to cause problems

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Code:
              string _recordStart = "HEADERSTART"; 
            public string RecordStart
              {
                  get { return _recordStart; }
                  set 
                  { 
                      _recordStart += value;
                      allRecords.Add(_recordStart);
                  }
              }
      Before ever setting RecordStart
      _recordStart == "HEADERSTAR T"
      Setting RecordStart the first time to "Bob"
      _recordStart == "HEADERSTARTBob ".
      allRecords[0] == "HEADERSTARTBob "
      Setting RecordStart the second time to "Neil"
      _recordStart == "BobNeil"
      allRecords[0] == "HEADERSTARTBob "
      allRecords[1] == "HEADERSTARTBob Neil"
      Setting RecordsStart the third time to "Fred"
      _recordStart == "HEADERSTARTBob NeilFred"
      allRecords[0] == "HEADERSTARTBob "
      allRecords[1] == "HEADERSTARTBob Neil"
      allRecords[2] == "HEADERSTARTBob NeilFred"
      Is that the behavior you are wanting?

      Comment

      • tig2810
        New Member
        • Aug 2008
        • 7

        #4
        HI
        I meant instead of 'allRecords.Add (_name); ' each time, should i be doing something like foreach fieldtype add to list etc?

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          I guess I'm not understanding the actual problem because it sounds like you are asking if there is a better way to add to a list than List.Add
          I need to maintain the order in the list.
          List.Add will add to the end of the list. The order of the list is the order in which you add. allRecords[0] will always be the first item added.

          A List<> maintains it order unless you re-order it in some way, such as sorting.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Code:
                    private string _example= "EXAMPLE_STRING"; 
                    public string Example
                    {
                        get { return _ example; }
                        set 
                        {
                            _example+= value; 
                            allRecords.Add(_example); 
                        }
                    }
            You do realize that when you send a value such as "Main Street" to Example you are going to get this...

            Before ever setting RecordStart
            _recordStart == "HEADERSTAR T"
            Setting RecordStart the first time to "Bob"
            _recordStart == "HEADERSTARTBob ".
            allRecords[0] == "HEADERSTARTBob "
            Setting RecordStart the second time to "Neil"
            _recordStart == "BobNeil"
            allRecords[0] == "HEADERSTARTBob "
            allRecords[1] == "HEADERSTARTBob Neil"
            Setting RecordsStart the third time to "Fred"
            _recordStart == "HEADERSTARTBob NeilFred"
            allRecords[0] == "HEADERSTARTBob "
            allRecords[1] == "HEADERSTARTBob Neil"
            allRecords[2] == "HEADERSTARTBob NeilFred"
            Setting Example to "Main Street"
            _example = "EXAMPLE_STRING Main String"
            _recordStart == "HEADERSTARTBob NeilFredEXAMPLE _STRINGMain String"
            allRecords[0] == "HEADERSTARTBob "
            allRecords[1] == "HEADERSTARTBob Neil"
            allRecords[2] == "HEADERSTARTBob NeilFred"
            allRecords[3] == "HEADERSTARTBob NeilFredEXAMPLE _STRINGMain String"

            You're asking about best practices and frankly I just can't imagine any scenario where you would want this sort of thing. After adding only a few items you could have List elements that are hundreds of characters long consisting primarily of the previous element's value.

            Comment

            • tig2810
              New Member
              • Aug 2008
              • 7

              #7
              the += is the required result for a weird EDI format. This is correct and was not the question. I'll look elsewhere. thanks anyway.

              Comment

              Working...