943,589 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1054
  • C# RSS
Jul 23rd, 2009
0

Unboxing an ArrayList in an object

Expand Post »
I'm probably going about this in the wrong way, but I have a stack of Objects, and some of these can be ArrayLists. I want to be able to pop an object that is actually an ArrayList off the stack, unbox it, add something, and then pop it back on. For example:

C# Syntax (Toggle Plain Text)
  1. Stack<Object> s = new Stack<Object>();
  2. ArrayList l = new ArrayList();
  3. Object x;
  4. s.Push(l);
  5. x = s.Pop();
  6. x = (ArrayList)x; //This is the line I don't know how to do
  7. x.Add("Something");
  8. s.push(x);

Does anyone have an idea of how to do what I'm trying to do?

Thanks in advance
Last edited by tdeck; Jul 23rd, 2009 at 11:16 am.
Similar Threads
Reputation Points: 17
Solved Threads: 0
Light Poster
tdeck is offline Offline
31 posts
since Jul 2009
Jul 23rd, 2009
1

Re: Unboxing an ArrayList in an object

do in this way:
C# Syntax (Toggle Plain Text)
  1. ...............
  2. Object x;
  3. x = s.Pop();
  4. ArrayList al=x as ArrayList ;
  5. if(al!=null)
  6. {
  7. al.Add("Something");
  8. }
  9. ...............
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Jul 23rd, 2009
0

Re: Unboxing an ArrayList in an object

Thanks, that works the way it should! Apparently what made the difference was the use of a new name for the created arrayList.
Reputation Points: 17
Solved Threads: 0
Light Poster
tdeck is offline Offline
31 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Comparing gridview with a datatable or gridview and highlight them
Next Thread in C# Forum Timeline: how to sort dictionary?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC