Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Best way to code the following

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff - 25 Sep 2007 14:40 GMT
I need to read about 100 lines of a csv file into memory.

Each line contains 6 fields.

What is the best way to store this into memory and then read it back.

Is a array of a multi- dimsion array the way to do this.

If so I can read the data into an array using an arraylist for each line
and the fields in a string array but cannot see how I can get the data
back out.

Looking forward to comments
amdrit - 25 Sep 2007 14:59 GMT
Your question is problematic because it is open ended and subjective.  What
does "Best Way" mean to you?

Personally, I would store the data in either a datatable or list<t> where t
= strongly typed class.

There are a bunch of samples on the web for reading and parsing CSV files.
These include an OLEDB solution, a regex solution, and plain 'ol procedural
parsing.

>I need to read about 100 lines of a csv file into memory.
>
[quoted text clipped - 9 lines]
>
> Looking forward to comments
Samuel R. Neff - 25 Sep 2007 15:24 GMT
Properly reading a CSV file can be a lot more complicated than simply
splitting strings.  Here's an excellent CSV reader.

A Fast CSV Reader
http://www.codeproject.com/cs/database/CsvReader.asp

HTH,

Sam

------------------------------------------------------------
We're hiring!  B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC.  Work with a variety of technologies
in a relaxed team environment.  See ads on Dice.com.

>I need to read about 100 lines of a csv file into memory.
>
[quoted text clipped - 9 lines]
>
>Looking forward to comments
Jeff - 25 Sep 2007 20:24 GMT
Its not the reading or writing the CSV file which is the issue it is I
cannot find an example of how to place data into a multi-demsendion
array and then how to get the data back out of the arral.

As far as I can see I must use an ArrayList so it can grow dynamically.

Regards
Jeff

> I need to read about 100 lines of a csv file into memory.
>
[quoted text clipped - 9 lines]
>
> Looking forward to comments
Peter Duniho - 25 Sep 2007 20:37 GMT
> Its not the reading or writing the CSV file which is the issue it is I
> cannot find an example of how to place data into a multi-demsendion
> array and then how to get the data back out of the arral.
>
> As far as I can see I must use an ArrayList so it can grow dynamically.

I think your question is still not very clear.

However, assuming ArrayList would be one possible implementation, you
should consider using the generic List<> class instead.  It provides
much of the same functionality, but as a safely typed collection object.

Of course, this assumes that the data you're putting into the list has a
consistent type.  If not, then perhaps ArrayList is the right solution
after all.

Pete
Jeff - 25 Sep 2007 21:04 GMT
Ok I give an example

My lines consists of

String1, string2, string3, string4, string5

So this would be a string[5] as it will always be 5 items in a line.

Now i add this to an arraylist

string[] myFields = new string[5];
ArrayList myFile = new ArrayList()
myFile.Add(myFields);

I can do this and build my array

The problem is how do i read the myFields from the myFile array
Cor Ligthert[MVP] - 25 Sep 2007 21:26 GMT
As amdrit wrote there are plenty of solutions, if you want to access a
multidimensional array, then in my idea just take a datatable, in
combination with the OleDB connection it is the most easiest thing to use.

www.connectionstrings.com

Cor

> Ok I give an example
>
[quoted text clipped - 13 lines]
>
> The problem is how do i read the myFields from the myFile array
Peter Duniho - 25 Sep 2007 21:45 GMT
> [...]
> string[] myFields = new string[5];
[quoted text clipped - 4 lines]
>
> The problem is how do i read the myFields from the myFile array

What part of that problem exactly are you having trouble with?

Do you understand the use of ArrayList generally?  That is, how you
would use it with any other object type?  If so, it's the same, where in
this case your object is a string array type (ie "string[]").

If you don't understand how to use ArrayList, you should say so.  That
way, we can tailor the answers to more usefully describe how to use
ArrayList.

Pete
Rick Lones - 26 Sep 2007 13:12 GMT
> Ok I give an example
>
[quoted text clipped - 13 lines]
>
> The problem is how do i read the myFields from the myFile array

foreach (string[] myStringArray in myFile)
{
    Console.WriteLine(myStringArray[0]);
    etc.
}

OR

for (int i = 0; i < myFile.Count; i++)
{
    string[] myStringArray = (string[])myFile[i];
    Console.WriteLine(myStringArray[0]);
    etc.
}

Is this what you are asking about - the syntax of accessing an ArrayList?

HTH,
-rick-
Samuel R. Neff - 25 Sep 2007 22:06 GMT
Perhaps the problem is your assumption that you want to put the data
into a multidimensional array in the first place.  Why do you want to
do this? What do you really want to do with the data?  Putting into
the array implies multi-pass handling of data (one pass to read it
from csv into array, and a second pass through the array to do
whatever you really want to do with the data).  If you can just get
directly from CSV to result then there is no need for the array.

Sam

------------------------------------------------------------
We're hiring!  B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC.  Work with a variety of technologies
in a relaxed team environment.  See ads on Dice.com.

>Its not the reading or writing the CSV file which is the issue it is I
>cannot find an example of how to place data into a multi-demsendion
[quoted text clipped - 4 lines]
>Regards
>Jeff
Jay Riggs - 26 Sep 2007 16:58 GMT
> Its not the reading or writing the CSV file which is the issue it is I
> cannot find an example of how to place data into a multi-demsendion
[quoted text clipped - 20 lines]
>
> - Show quoted text -

Jeff, others have given you great advice.

Here are some specifics that might help.

First, create a class to store each record from your csv.  Something
like this:

   public class Record
   {
       public Record(string field1, string field2, string field3) {
           _field1 = field1;
           _field2 = field2;
           _field3 = field3;
       }
       private string _field1;
       private string _field2;
       private string _field3;

       public void DoSomething() {
           Console.WriteLine("{0}, {1} and {2}",
                             _field1, _field2, _field3);
       }
   }

As stated in the replies to your original message, the recommended
(and .NET 2.0+) approach is to use Generics (List<T> specifically).

   List<Record> _myRecordsGen = new List<Record>();
   // Create a list of Record objects.  (You mentioned you had no
problem extracting
   // data from your data source).
   _myRecordsGen.Add(new Record("f1 of 1 (gen)", "f2 of 1 (gen)", "f3
of 1 (gen)"));
   _myRecordsGen.Add(new Record("f1 of 2 (gen)", "f2 of 2 (gen)", "f3
of 2 (gen)"));
   _myRecordsGen.Add(new Record("f1 of 3 (gen)", "f2 of 3 (gen)", "f3
of 3 (gen)"));

   // Get your Record objects out of the list (and do something with
each).
   foreach (Record record in _myRecordsGen) {
       record.DoSomething();
   }

You mentioned you think you might need to use an ArrayList.  This is
true if you're using .NET 1.x.  Here's the same example using an
ArrayList.

   ArrayList _myRecordsAL = new ArrayList();
   // Create a list of Record objects.
   _myRecordsAL.Add(new Record("f1 of 1 (al)", "f2 of 1 (al)", "f3 of
1 (al)"));
   _myRecordsAL.Add(new Record("f1 of 2 (al)", "f2 of 2 (al)", "f3 of
2 (al)"));
   _myRecordsAL.Add(new Record("f1 of 3 (al)", "f2 of 3 (al)", "f3 of
3 (al)"));

   // Get your Record objects out of the list (and do something with
each).
   foreach (Record record in _myRecordsAL) {
       record.DoSomething();
   }

HTH
-Jay
mark4asp - 26 Sep 2007 07:17 GMT
>I need to read about 100 lines of a csv file into memory.
>
[quoted text clipped - 9 lines]
>
>Looking forward to comments

You haven't given enough details. However, I think an array would be a
bad idea - surely each column in the csv could have a different
datatype?  I would personally use a List<T> for this. You can use the
set property of your List<T> object to naturally validate data entry to
the list. In general, I prefer List<T> to ArrayList because List<T> is
easier to use - none of those irritating casts to bother with.
Jeff - 29 Sep 2007 13:42 GMT
Thanks all I have learn't from this.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.