Solved

Use of a multidemensional Array

Posted on 2009-07-12
8
223 Views
Last Modified: 2012-05-07
I am using c# to write a program,
i have the data that i need to use in a text file, that is saves as a comma seperated list, with multiple lines

Ex

s[0] = {a,b,c}
s[1] = {d,e,f}
s[2] = {g,h,i}

I am looking for how to pull in this data as a multidemensional array, or whatever would be the easiest thing to use.

any suggestions?
0
Comment
Question by:cotj73
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 6

Expert Comment

by:microbolt
ID: 24835331
What I like to do is create an class that will hold each line of the csv.

For example if you had an CSV with 3 fields called id, firstname, lastname make an class like this:

class person
{
      public int id { get; set; }
      public string firstname { get; set; }
      public string lastname { get; set; }
}

Then create an List<T> of the collection.

     List<person> Persons = new List<person>();

The go through each line of the CSV and add it to the List

     Persons.Add(new person() { id = csv[0], firstname = csv[1], lastname = csv[2] });

Whats nice about this approach is you can use a number of different approaches for accessing the data like foreach or LINQ.  For example if you wanted to use LINQ against it you could type:

     var results = from p in Persons
                          where p.id == 5
                          select p;

or an foreach

     foreach(var currentPerson in Persons)
     {
            MessageBox.Show(currentPerson.firstname);
     }
0
 
LVL 1

Author Comment

by:cotj73
ID: 24851271
the only problem that i forsee in this approach is that i have to read about 325 lines, and each line holds about 40 different values.
here is an example of a line

-.2,-.2,19785,0,T 04/17/2009,13:35:28.953000 UTC,first,middle,last,1946-11-11,Gender,OD,,ZERNIKE,SCALE:  2.56245844477293E-03,28,0,7.26319504749052E-04,-1.28435703005351E-04,-4.70736639230834E-04,-2.11696194741656E-04,-6.94188170158255E-06,1.21760327335195E-05,-1.16471189175351E-04,1.892881053328E-04,2.43394089421797E-05,-4.27588262279435E-06,3.96428568852939E-05,1.21416458423661E-05,-5.07598197431613E-06,2.42835581669891E-05,1.84678127720032E-05,-1.6793619674676E-05,-5.20898049006682E-06,-1.76892518358479E-05,-7.00485451263667E-06,1.38926890110059E-05,-1.61087650649244E-05,1.32222083318142E-05,2.28048832601142E-05,4.92909574194228E-05,-3.9719165605381E-05,-3.30874305806735E-05,4.36658825006991E-05,-1,0,-1,-5.05906053272166E-03,-3.51320393781458E-03,6.18699368903292E-04,4.7028011256734E-04,1003597,

0
 
LVL 6

Expert Comment

by:microbolt
ID: 24851295
Won't be any problem with it just make 40 properties in the class.  Another benefit that you might like since it looks like measurements your working with is with this approach you can easily sort based on column as well.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 1

Author Comment

by:cotj73
ID: 24851547
would it be possible in this class to use a for loop in the declaration,
many of the values of the text are
z00
z10
z11
z20
z21
z22
...
z65
z66

i have an algorythm to do this
for (int i = 0; i <= iFValue; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    dgvSlectedPatient.Columns.Add("cZ" + Convert.ToString(i) + Convert.ToString(j), "Z" + Convert.ToString(i) + Convert.ToString(j));
                }
            }

i am using this elseware. would this work if i used it instead of adding colums have it sequence through
or does that not work for classes
0
 
LVL 1

Author Comment

by:cotj73
ID: 24851611
sorry here is a better look

for (int i = 0; i <= 6; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    "cZ" + Convert.ToString(i) + Convert.ToString(j);
                }
            }

and will this method work of all of the lines that i have to read
0
 
LVL 6

Expert Comment

by:microbolt
ID: 24851773
Another option you have is creating an array in the class just for the cZ items and using the properties for the rest.  Like this:


            List<person> Persons = new List<person>();
            person newPerson = new person();
 
            for (int i = 0; i <= 6; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    newPerson.cz[int.Parse(Convert.ToString(i) + Convert.ToString(j))] = "Set the value for each field here";
                }
            }
 
            Persons.Add(newPerson);

Open in new window

0
 
LVL 6

Accepted Solution

by:
microbolt earned 500 total points
ID: 24851779
And declare the class like this:
class person
    {
        public int id { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public string[] cz {get; set;}
 
        public person()
        {
            cz = new string[67];
        }
    }

Open in new window

0
 
LVL 1

Author Closing Comment

by:cotj73
ID: 31602593
Great Help, Thank You
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VS2010 Build fails to install 14 102
asp.net, radiobuttonlist, c# 3 69
What is GIS method of Geometry data type? 6 36
How does this modal work? 3 33
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question