Unboxing an ArrayList in an object

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 31
Reputation: tdeck is an unknown quantity at this point 
Solved Threads: 0
tdeck tdeck is offline Offline
Light Poster

Unboxing an ArrayList in an object

 
0
  #1
Jul 23rd, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Unboxing an ArrayList in an object

 
1
  #2
Jul 23rd, 2009
do in this way:
  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. ...............
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 31
Reputation: tdeck is an unknown quantity at this point 
Solved Threads: 0
tdeck tdeck is offline Offline
Light Poster

Re: Unboxing an ArrayList in an object

 
0
  #3
Jul 23rd, 2009
Thanks, that works the way it should! Apparently what made the difference was the use of a new name for the created arrayList.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 498 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC