On Sat, 29 Dec 2007 18:49:38 -0800, John Rogers <jo************@aol.com>
wrote:
[...]
After you create your struct and pass the values to the struct, how do
you access
the values?
By accessing an instance of the struct and referencing the public member
fields.
I was assuming that it would be like this
// create my list
List<NodeInfoSavedTree = new List<NodeInfo>();
This creates a List that can contain instances of NodeInfo. It's sort of
like initializing an array like this:
NodeInfo[] array = new NodeInfo[10];
except that you don't need to know how many elements you want in advance..
// add information to the list
SavedTree.Add(new NodeInfo(node.Text, depth));
This initializes an instance of NodeInfo and pass that instance to the
List.Add() method. At this point the List now contains a copy of that
instance of NodeInfo. It's sort of like assigning a value to an array
element:
array[5] = new NodeInfo(node.Text, depth);
except that you don't have to keep track of where the current place to add
an element in the array is, and if the data structure becomes filled, it
automatically reallocates itself internally when you try to add more.
// access the info like this, but this does not work.
listBox.Add(SavedTree.name);
The SavedTree variable is the list itself. It doesn't have a "name"
property or field.
The semantics of the List<class are similar in many respects to arrays..
Just as you have to index an element in an array to get at the values
stored there, you have to index an element in the List<to get at the
values stored there. The List<itself doesn't know anything about those
values any more than an array would.
The code I posted shows how you _do_ get at the information in the
NodeInfo instances. See the RestoreTree() method for examples.
This is strange, thats all I have to say.
I don't know why it's strange. Either you're not getting enough sleep, or
you need to get back to basics and review the basic .NET data structures
(or maybe even C arrays). :)
Pete