C# Dynamically Named Array (Or arraylists)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • buggtb
    New Member
    • Apr 2007
    • 7

    #1

    C# Dynamically Named Array (Or arraylists)

    Hi Guys is it possible to do something along the lines of
    Code:
    for (i=0; i < (namearray.length); i++)
    {
    string[] namearray[i]
    }
    I'm wanting to create a number of arrays dynamically depending on how many variables get loaded into my namearray, I would also like the arrays to be called whatever the variable in the array is called.

    so for example:
    Code:
    namearray[1] = Tom
    namearray[2] = Dick
    namearray[3] = harry
    I would like my arrays then to be called

    Tom, Dick and Harry

    Anyhelp appreciated,

    cheers

    Tom
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by buggtb
    Hi Guys is it possible to do something along the lines of
    Code:
    for (i=0; i < (namearray.length); i++)
    {
    string[] namearray[i]
    }
    I'm wanting to create a number of arrays dynamically depending on how many variables get loaded into my namearray, I would also like the arrays to be called whatever the variable in the array is called.

    so for example:
    Code:
    namearray[1] = Tom
    namearray[2] = Dick
    namearray[3] = harry
    I would like my arrays then to be called

    Tom, Dick and Harry

    Anyhelp appreciated,

    cheers

    Tom
    Could you explain what your trying to do a bit more?

    Variable names are used by programmers to easily refer to the data they are using to solve their problem. It doesn't make sense to me why you would want to name a variable dynamically.

    -Frinny

    Comment

    • buggtb
      New Member
      • Apr 2007
      • 7

      #3
      Sorry,

      I don't want to dynamically name variables, as you point out that would be a bit silly, what I am doing is loading a csv file(which I have accomplished) depending on the number of 'columns' in this file I would like that amount of arrays generated, with the column header names as the array names to make my naming policy and later coding easier, as everything referring to that column will have a similar name.

      At the moment all the column headers are loaded into a array called headers, so for example in headers there is the variables, Col1, Col2, Col3.

      Now I would like to be able to create 3 array's with the array names of Col1, Col2, Col3, now if these values were always the same it wouldn't matter but as there are a number of different csv files to load this can alter.

      Also I want all the values in columised arrays' so I can work on the figures on a column by column basis.

      Hope this makes some sense


      Cheers

      Tom

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by buggtb
        Sorry,

        I don't want to dynamically name variables, as you point out that would be a bit silly, what I am doing is loading a csv file(which I have accomplished) depending on the number of 'columns' in this file I would like that amount of arrays generated, with the column header names as the array names to make my naming policy and later coding easier, as everything referring to that column will have a similar name.

        At the moment all the column headers are loaded into a array called headers, so for example in headers there is the variables, Col1, Col2, Col3.

        Now I would like to be able to create 3 array's with the array names of Col1, Col2, Col3, now if these values were always the same it wouldn't matter but as there are a number of different csv files to load this can alter.

        Also I want all the values in columised arrays' so I can work on the figures on a column by column basis.

        Hope this makes some sense


        Cheers

        Tom
        For some reason I think that you might have better luck solving your problem using something like a DataSet. More information on DataSets can be found here

        There are many solutions to your problem.
        You cold store the header names in an array like you're doing and then store the column information in an array of arrays...
        Then search through the header names for the index of the column you're looking for ....
        Then retrieve the column information from the array for that column (stored in the array of arrays)
        Just make sure that the indexes match up (ie column header at index 3 in the header array matches with the array stored in the array of arrays at index 3)

        Good Luck
        -Frinny

        Comment

        • buggtb
          New Member
          • Apr 2007
          • 7

          #5
          Just for future reference a guy on another forum came up with the answer,

          string[] arrayNames = new string[fieldCount]; // arraySize = headers.Length?
          for(int i = 0; i < headers.Length; i++) {
          arrayNames[i] = headers[i];
          }

          This appears to create arrays named with whats in my headers array, and the length of the data in my data arrays.


          Cheers

          Tom

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by buggtb
            Just for future reference a guy on another forum came up with the answer,

            string[] arrayNames = new string[fieldCount]; // arraySize = headers.Length?
            for(int i = 0; i < headers.Length; i++) {
            arrayNames[i] = headers[i];
            }

            This appears to create arrays named with whats in my headers array, and the length of the data in my data arrays.


            Cheers

            Tom
            Thanks for sharing the solution Tom!

            Cheers!

            -Frinny

            Comment

            Working...