Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does Array class implement IList interface?

Status
Not open for further replies.

polocar

Programmer
Sep 20, 2004
89
IT
Hi all,
I'm writing a C# program (with Visual Studio .NET 2003 Professional), and I created an array of TreeNode objects using the simple statement:

TreeNode[] atn;

Then I created a TreeNode object with the statement

TreeNode tn = new TreeNode("test");

and I tried to add it to the array, but I discovered that there isn't a method Add: in fact, the statement

atn.Add(tn);

generates a compile error.

The strange thing is that, when I consulted the IList interface in the MSDN library (the interface that implements Add, Insert, Remove, RemoveAt and other methods), I read that it can be used from the Array class...

So, why the compiler gives me an error if I try to use the Add method to add an element to an array?
If this way isn't possible, how can I increase the dimension of a dynamic array and add an element to it?

Thank you very much
 
P.S.: I have seen that the ArrayList class implements all the IList methods, but I need to use a simple Array because then I must add it to a TreeView object using the AddRange method of the TreeNodeCollection class (and it requires a TreeNode[] class...)

tv.Nodes.AddRange(atn);
 
Use ArrayList then copy to Array
ArrayList al = new ArrayList(50);
TreeNode[] atn;
al.Add(xxxx);
.......

' something like
TreeNode[] wa = new TreeNode[al.Count - 1];
al.CopyTo(wa)
atn = wa;

Compare Code
 
If Microsoft had been thinking ahead on this, they would have added an overload to AddRange that accepted a TreeNodeCollection.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top