corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » C# multidimensional array


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author C# multidimensional array
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Offline
22nd Nov 11 at 20:39   View Garage View User's Profile U2U Member Reply With Quote

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
22nd Nov 11 at 21:13   View User's Profile U2U Member Reply With Quote

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
22nd Nov 11 at 21:23   View User's Profile U2U Member Reply With Quote

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

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Offline
22nd Nov 11 at 21:26   View Garage View User's Profile U2U Member Reply With Quote

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
22nd Nov 11 at 21:39   View User's Profile U2U Member Reply With Quote

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
22nd Nov 11 at 21:54   View User's Profile U2U Member Reply With Quote

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
22nd Nov 11 at 22:50   View User's Profile U2U Member Reply With Quote

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
22nd Nov 11 at 22:59   View User's Profile U2U Member Reply With Quote

I could probably answer this question in PHP if that helps
Paul_J
Member

Registered: 6th Jun 02
Location: London
User status: Offline
22nd Nov 11 at 23:11   View User's Profile U2U Member Reply With Quote

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
22nd Nov 11 at 23:14   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Paul_J





Auto implemented properties FTW!

I wish Python was that helpful.

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
can anyone help with some C++ stuff??? SteveW Geek Day 20 1094
27th Aug 03 at 20:55
by corb
 
Help desperatly needed from c programmers kerzo Geek Day 24 1888
20th Nov 03 at 01:53
by Jason
 
what is up with this (comp peeps) groom General Chat 0 734
14th Dec 03 at 18:14
by groom
 
PHP Gurus Dom Geek Day 7 243
23rd Nov 09 at 17:33
by Dom
 
Website help again! Simon Geek Day 20 335
14th Jul 11 at 16:58
by Simon
 

Corsa Sport » Message Board » Off Day » Geek Day » C# multidimensional array 29 database queries in 0.0156319 seconds