Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
Just checking I'm giving the right advice on something.
If you had for example a CSV text file and you read each item in a single line in to an array named elements[], would you then declare another array named lines[] to store each of instance of elements[] or would you just have a multidimensional array with one name and additional indices?
Like data[][]
The first approach kind of sounds like it might make sense but I can't find any reference to it in documentation. You can set up multidimensional and jagged structures but they're only ever named as one thing.
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
I'd personally create some objects. You are in C# after all and use some form of collection depending on the needs. So for example, if the lines you're importing are say sales invoices, you end up with a collection of sales invoices.
This has plenty of advantages over just doing 2 arrays, as it's typed (with objects) and you can use things like LINQ to organize or arrange the data.
For example, if I know I'm going to have a csv file import (a fixed column format) like a list of sales invoices: InvoiceNumber, InvoiceDate, Customer, Detail, TotalValue
I'd create a class called 'SalesInvoice' and give it property's for InvoiceNumber, InvoiceDate, Customer, Detail, TotalValue.
I can create some form of collection to hold all these SalesInvoices:
e.g. List<SalesInvoice> SalesInvoiceCollection = new List<SalesInvoice>();
Then in my code where I want to do the import, I could do something where I loop through each line I'm importing.
Inside this, I can then create a new sales invoice:
e.g. SalesInvoice newSalesInvoice = new SalesInvoice();
Then map the imported line values to the object values of the invoice (I could have even passed it through on the constructor of the SalesInvoice and had a method inside the object do this for me):
newSalesInvoice.InvoiceNumber = ImportedValue[0];
newSalesInvoice.InvoiceDate = ImportedValue[1];
newSalesInvoice.Customer = ImportedValue[2];
newSalesInvoice.Detail = ImportedValue[3];
newSalesInvoice.TotalValue = ImportedValue[4];
Then I'll add the invoice to the collection,
e.g.
SalesInvoiceCollection.Add(newSalesInvoice);
... Once I've finished looping through all the lines I've got a populated collection, full of objects which are populated with data. I can now use these in any way I like and pass it around my app.
This is a bit of a sketchy example, as it's not really how I'd do things (I wouldn't be importing a csv file without at least a header line in the file to help match the columns to the objects, or would advise a different import format (e.g. xml) csv and indexes can be quite error prone (the wrong data in the wrong columns / bad data). I would also want to validate the data further and depending on your use case, the list collection may not be what you want.
However, the point I am making is that not using objects is generally bad practice in C#. Given your example, data[][] and using indexes is quite ambiguous to anyone who comes along and wants to maintain this code, or even the original developer may regret it when things change. Plus in C# you get intellisense and compiler checking against the objects. Also helps when debugging too.
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
Although, I'll just add, if something is particularly performance critical - doing what I said may not be advised. However, I'm talking importing millions of lines.
If you did want to do the array, thing, you could just do a fairly loose string array for each line (where the line input is being split by the delimiter).
Then just have an array of string arrays (which contains the lines)
However, even then I'd prefer to have a collection of string arrays and even further than that prefer to have a collection of importLines objects which at worst contain a string array and at best contain the definition of the imported Lines.
|
Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
The array of string arrays - is that referred to using two names?
Everything I've seen just makes it one big array with one name.
|
A2H GO
Member
Registered: 14th Sep 04
Location: Stoke
User status: Offline
|
I'd love to be able to understand that the fucks going on in here.
|
Edd
Member
Registered: 8th Nov 04
Location: Glasgow
User status: Offline
|
quote: Originally posted by A2H GO
I'd love to be able to understand that the fucks going on in here.
Paul doesn't even understand what's going on here
|
James
Member
Registered: 1st Jun 02
Location: Surrey
User status: Offline
|
quote: Originally posted by A2H GO
I'd love to be able to understand that the fucks going on in here.
It's really not that exciting.
|
Sam
Moderator Premium Member
Registered: 24th Dec 99
Location: West Midlands
User status: Offline
|
I could probably answer this question in PHP if that helps
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
quote: Originally posted by Ian
The array of string arrays - is that referred to using two names?
To be honest, to do that, just create a new class with your string array in it:
e.g. simple LineImport class (although I'd prefer it to have the elements defined as properties rather than string array)
Then just create a list of the LineImport and add new lines per line imported to the list.
As you can see in this basic example, of importing 1 fake line, you now have your 2 dimensional collection, you can see Lines currently contains a single line with the 4 imported elements.
Taking it a step further,
Make sense?
Oh and if a List is too heavy, use an IEnumerable for the collection.
[Edited on 22-11-2011 by Paul_J]
|
James
Member
Registered: 1st Jun 02
Location: Surrey
User status: Offline
|
quote: Originally posted by Paul_J
Auto implemented properties FTW!
I wish Python was that helpful.
|