Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
We have a class:
class Node
    {
        private string gid;
        private string name;

public Node(string gimid, string ptyname)  // constructor
 {
     gid = gimid;
     name = ptyname;
 }

        public string gmid          // property
        {
            get { return gid; }
            set { gid = value; }
        }

        public string pname         // property
        {
            get { return name; }
            set { name = value; }
        }

}

And an ArrayList that will be holding above table’s structure to store records as follows:
ArrayList mapList = new new ArrayList(); 
string FirstElementID = ds.Tables[0].Rows[0]["Pty_id"].ToString();                          string FirstElementName = ds.Tables[0].Rows[0]["Pty_Name"].ToString();    
Node FirstNode = new Node(FirstElementID, FirstElementName);    
 	mapList.Add(FirstNode);                        


Now the question is how to access elements from this ArrayList?
Posted
Updated 5-Jun-11 15:26pm
v3
Comments
Wonde Tadesse 5-Jun-11 16:06pm    
Which part is multidimensional ArrayList ? I can only see a single dimensional ArrayList.
Sergey Alexandrovich Kryukov 5-Jun-11 21:28pm    
Imaginary :-)
--SA
Wonde Tadesse 5-Jun-11 23:16pm    
I don't get it.
Sergey Alexandrovich Kryukov 5-Jun-11 23:33pm    
I mean you were perfectly right, nothing multidimensional, my note was sarcastic, you could pay attention for the smile.
By the way, to me all dimensions of all structures are guess what? They are all zero-dimensional. It depends on the definition of dimension.
--SA
Wonde Tadesse 5-Jun-11 23:41pm    
I get it.

You can rewrite it to

C#
List<Node> mapList = new List<Node>()

foreach (DataRow row in ds.Tables[0].Rows)
{
  mapList.Add(new Node(row["Pty_id"].ToString(), row["Pty_Name"].ToString()));
}


And when you are done adding elements to the List. You can access, add, insert and modify the List as needed.

But it has nothing to do with a multidimensional array.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jun-11 21:28pm    
My 5.
--SA
Kim Togo 6-Jun-11 2:44am    
Thanks SA
I suggest you replace ArrayList with its generic counterpart List<T> where T is the type of elements in the list. In your code that would be Node. Please read about it here: http://en.csharp-online.net/CSharp_Generics_Recipes%E2%80%94Replacing_the_ArrayList_with_Its_Generic_Counterpart[^].
That way when you access an element from that list the type will already be known and you can access all of Node's members directly without using a cast operation.

Cheers!
 
Share this answer
 
Comments
Espen Harlinn 5-Jun-11 6:12am    
Good point, my 5
Manfred Rudolf Bihy 5-Jun-11 7:54am    
Thank you!
Kim Togo 5-Jun-11 8:36am    
Good point, my 5. Replacing ArrayList with List<T> is much better.
Sergey Alexandrovich Kryukov 5-Jun-11 21:27pm    
Sure, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900