How do I index into a List<Array>?

This is a discussion on How do I index into a List<Array>? within the C# Programming forums, part of the General Programming Boards category; I want a dynamic multidimensional array of strings. I searched the forum a bit and it seems that a List<Array> ...

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Question How do I index into a List<Array>? *SOLVED*

    I want a dynamic multidimensional array of strings. I searched the forum a bit and it seems that a List<Array> is what I want to use. Here's an idea of what I'm doing:

    Code:
    List<Array> timetrackview = new List<Array>();
    
    //Get some rows from SQL and store results in:
    String[] ttvData = new String[8];
    //then add string array to timetrackview
    timetrackview.Add(ttvData);
    
    //since I know the second returned column is an int stored in my array I want to store it somewhere else
    lastSR = int.Parse(timetrackview[0][1]);
    
    //except this doesn't work
    I cannot use a foreach; any help would be appreciated.
    Last edited by Dragoon_42; 04-25-2008 at 09:20 AM.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    it would be
    Code:
    timetrackview[0].GetValue(1).ToString();
    or have a List<List<sometype>>;
    Last edited by indigo0086; 04-25-2008 at 09:19 AM.

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    You sir, are my hero. Thanks!

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    with a List<List<sometype>> you can have a dynamic array in both dimensions and use the two dimensional index operation and are better typed

    Then there are beautiful things called extension methods. You put these public static methods in public static classes and mark the first parameter with the "this" keyword so any objec within it's scope looks at it as a member value. Example

    Code:
    	public static class Extensions
    	{
    		public static int Get2d(this List<Array> x, i1, i2) //this indicates implicit parameter
    		{
    			//bounds checking perhaps
    			return (int)x[i1].GetValue(i2);
    		}
    	}
    now you can call it as such
    Code:
    tmp.Get2d(0, 1)
    See you're calling the Get2d method, but without passing the list as the first parameter, all List<Array>s sees it as a member method.

  5. #5
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Now that's pretty slick. Luckily for me in this instance I only need the dynamicness of the list. Since I know that the array inside will always only need 8 cells. I'll try to remember this if I need a truly dynamic multidimensional array.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    then why not have it a List<string[]>

  7. #7
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    I had tried that, but kept getting errors. This way I posted above worked fine for me.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    perhaps you initialized it List<string[8]>?

  9. #9
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    I think what I'd tried was List<string[]>=new List<string[8]>(), but I don't remember for sure and that code is long gone now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Reiventing the wheel (again)
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2007, 03:26 AM
  3. Getting index of chosen item in the combo box
    By what3v3r in forum Windows Programming
    Replies: 4
    Last Post: 09-01-2006, 12:49 PM
  4. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 06:45 AM
  5. file index update error
    By daluu in forum C Programming
    Replies: 1
    Last Post: 04-28-2003, 03:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21