adding collection to another collection...

  • Thread starter Thread starter Adam Right
  • Start date Start date
A

Adam Right

How can i add a collection to another collection ?

For example :
---------------------------------------------
StringCollection strColl= new StringCollection(); //string series
Hashtable hshCol= new Hashtable(); // main collection for string series

strColl.Add("white"); //first series starting
strColl.Add("hot");
strColl.Add("little");
hshCol.Add(1, strColl); //the first one adding second one(to hashtabe)

strColl.Clear();
strColl.Add("red"); //second series
strColl.Add("cold");
strColl.Add("big");
hshCol.Add(2, strColl);// second series has been added second collection
-------------------------------------------------

In this situation, can i get string series via the key value of hashtable
collection (hshCol) ?
I need string values of strColl from hshCol , is it possible ?
Thanks ...

Adam
 
Adam said:
How can i add a collection to another collection ?

Well, one way would be to do it just as the code you posted.
For example :
---------------------------------------------
StringCollection strColl= new StringCollection(); //string series
Hashtable hshCol= new Hashtable(); // main collection for string series

strColl.Add("white"); //first series starting
strColl.Add("hot");
strColl.Add("little");
hshCol.Add(1, strColl); //the first one adding second one(to hashtabe)

StringCollection is, like all objects in C#, derived from Object and can
be added to a HashTable just like any other object. So, nothing's wrong
with the above code.
[...]
In this situation, can i get string series via the key value of hashtable
collection (hshCol) ?

Yes. The same way you'd get any other object from the HashTable. For
example:

StringCollection strColl = hshCol[1] as StringCollection;
I need string values of strColl from hshCol , is it possible ?

See above. You've already posted most of the code related to
accomplishing what you seem to want, so it's not clear to me what
question you actually have. You seem to already know the answer. If
there's something specific giving you trouble, perhaps you could be more
precise about what that is.

Pete
 
Hashtables are depricated. Use the Dictionary and things might be a
bit more clear for you.
 
not_a_commie said:
Hashtables are depricated. Use the Dictionary and things might be a
bit more clear for you.

I don't see anything in MSDN saying that the Hashtable class is
deprecated. I also don't see how using a Dictionary<> instead would
make the issue "a bit more clear". The two behave in very similar ways
and someone who is confused using one is likely to be confused using the
other.

Pete
 
Adam Right said:
How can i add a collection to another collection ?

For example :
---------------------------------------------
StringCollection strColl= new StringCollection(); //string series
Hashtable hshCol= new Hashtable(); // main collection for string series

strColl.Add("white"); //first series starting
strColl.Add("hot");
strColl.Add("little");
hshCol.Add(1, strColl); //the first one adding second one(to hashtabe)

strColl.Clear();
strColl.Add("red"); //second series
strColl.Add("cold");
strColl.Add("big");
hshCol.Add(2, strColl);// second series has been added second collection
-------------------------------------------------

In this situation, can i get string series via the key value of hashtable
collection (hshCol) ?
I need string values of strColl from hshCol , is it possible ?

Just use:

StringCollection collection = (StringCollection) hshCol[1];

(Or whichever key you want.)

If you're just going to use sequential integer keys, however, an array
would be simpler.
 
Back
Top