Best way to code the following

J

Jeff

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
 
A

amdrit

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.
 
J

Jeff

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
 
P

Peter Duniho

Jeff said:
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
 
J

Jeff

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
 
C

Cor Ligthert[MVP]

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
 
P

Peter Duniho

Jeff said:
[...]
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

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
 
S

Samuel R. Neff

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
 
M

mark4asp

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

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.
 
R

Rick Lones

Jeff said:
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

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

OR

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

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

HTH,
-rick-
 
J

Jay Riggs

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









- 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top