473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. Class myclass()
  2. {
  3.  
  4.         public List<string> allRecords = new List<string>();
  5.  
  6.         // Basically i am appending whatever is set to the end of the string with all these
  7.  
  8.         string _recordStart = "HEADERSTART"; 
  9.         public string RecordStart
  10.         {
  11.             get { return _ recordStart; }
  12.             set 
  13.             { 
  14.                 _ recordStart += value;
  15.                 allRecords.Add(_recordStart);
  16.             }
  17.         }
  18.  
  19.         private string _example= "EXAMPLE_STRING"; 
  20.         public string Example
  21.         {
  22.             get { return _ example; }
  23.             set 
  24.             {
  25.                 _example+= value; 
  26.                 allRecords.Add(_example); 
  27.             }
  28.         }
  29.  
  30. // Many more encapsulated fields
  31.  
  32.         string _recordEnd= "HEADEREND"; 
  33.         public string HdrRecordEnd
  34.         {
  35.             get { return _ recordEnd; }
  36.             set 
  37.             { 
  38.                 _ recordEnd += value;
  39.                 allRecords.Add(_recordEnd);
  40.             }
  41.         }
  42. }
May 30 '09 #1
6 2739
tlhintoq
3,525 Expert 2GB
@tig2810
Lines 14 and 38. That space between the underscore and recordEnd is going to cause problems
May 30 '09 #2
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1.         string _recordStart = "HEADERSTART"; 
  2.       public string RecordStart
  3.         {
  4.             get { return _recordStart; }
  5.             set 
  6.             { 
  7.                 _recordStart += value;
  8.                 allRecords.Add(_recordStart);
  9.             }
  10.         }
  11.  
Before ever setting RecordStart
_recordStart == "HEADERSTART"
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] == "HEADERSTARTBobNeil"
Setting RecordsStart the third time to "Fred"
_recordStart == "HEADERSTARTBobNeilFred"
allRecords[0] == "HEADERSTARTBob"
allRecords[1] == "HEADERSTARTBobNeil"
allRecords[2] == "HEADERSTARTBobNeilFred"
Is that the behavior you are wanting?
May 30 '09 #3
HI
I meant instead of 'allRecords.Add(_name); ' each time, should i be doing something like foreach fieldtype add to list etc?
May 30 '09 #4
tlhintoq
3,525 Expert 2GB
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.
May 30 '09 #5
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1.         private string _example= "EXAMPLE_STRING"; 
  2.         public string Example
  3.         {
  4.             get { return _ example; }
  5.             set 
  6.             {
  7.                 _example+= value; 
  8.                 allRecords.Add(_example); 
  9.             }
  10.         }
  11.  
  12.  
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 == "HEADERSTART"
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] == "HEADERSTARTBobNeil"
Setting RecordsStart the third time to "Fred"
_recordStart == "HEADERSTARTBobNeilFred"
allRecords[0] == "HEADERSTARTBob"
allRecords[1] == "HEADERSTARTBobNeil"
allRecords[2] == "HEADERSTARTBobNeilFred"
Setting Example to "Main Street"
_example = "EXAMPLE_STRINGMain String"
_recordStart == "HEADERSTARTBobNeilFredEXAMPLE_STRINGMain String"
allRecords[0] == "HEADERSTARTBob"
allRecords[1] == "HEADERSTARTBobNeil"
allRecords[2] == "HEADERSTARTBobNeilFred"
allRecords[3] == "HEADERSTARTBobNeilFredEXAMPLE_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.
May 30 '09 #6
the += is the required result for a weird EDI format. This is correct and was not the question. I'll look elsewhere. thanks anyway.
May 31 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: yvan | last post by:
Approximately once a month, a client of ours sends us a bunch of comma-delimited text files which I have to clean up and then import into their MS SQL database. All last week, I was using a Cold...
1
by: Chris Uwins | last post by:
Hi there, i know theres a number of ways I can achieve this but want to know the best, (but still quite simple). Up until a year ago I never used Access but have designed a few databases for...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
2
by: Robert Vasquez | last post by:
I have three classes. One (Class ObjectC) and two other classes (Class1 and Class2) that will hold instances of the ObjectC class. I would like to transfer an instance of OjectC from Class1 into...
4
by: Guy Noir | last post by:
Hello. Is there a pattern or best practice for the following scenario? I have a list of items I would like to compare. The number of items are decided at runtime. ObjectA, ObjectB,...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
5
by: blisspikle | last post by:
I figure that someone good at dotnet can look at this and give me a clue on how to easily organize this code? If there is a unique identifier like "Publisher" with a bunch of "Book" that are...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
6
by: kamsmartx | last post by:
I'm new to programming and need some help figuring out an algorithm. I need to design some kind of algorithm which will help us define capacity for one of our portfolios....here's the problem...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.