yusufozkay
July 18th, 2005, 04:56 AM
how can I convert object to arraylist ?
|
Click to See Complete Forum and Search --> : how can I convert object to arraylist ? yusufozkay July 18th, 2005, 04:56 AM how can I convert object to arraylist ? torrud July 18th, 2005, 05:05 AM //if obj is your ArrayList-object ArrayList myList = obj as ArrayList; if (null != myList) { //do something with your list } exterminator July 18th, 2005, 05:49 AM How about simple casting. Like suppose you store an arraylist into a session collection then what it returns is an object type. So, for it to be usable we need to get it casted to the original type it was.So you could simply do:ArrayList myArrayList = (ArrayList) obj;where obj is retrieved from Session by something like://put into session Session["ArrayList"] = someArrayList; //retrieve from session into an Object obj; obj = Session["ArrayList"]; //and then back into our ArrayList type as mentioned aboveHope this helps. Don't forget to rate good/helpful posts. :thumb: yusufozkay July 18th, 2005, 06:55 AM I put ArrayLists into an ArrayList. I send this ArrayList to a class. In that class I will use the ArrayLists. In this class every time the timer elapsed I will get the next ArrayList. I can't use these ways I think. torrud July 18th, 2005, 07:24 AM Hm, I can not see any problems. I posted a safety cast and exterminator posted a simply cast which you have to put in a try-catch-block maybe. But you MUST use one of this possibilities if you want to get access to your items in the ArrayLists inside of your ArrayList. If I am wrong and you find another good way, I am really interested in. yusufozkay July 18th, 2005, 07:38 AM hmm yes "ArrayList myArrayList = (ArrayList) obj;" this works, thank you. jhammer July 18th, 2005, 08:18 AM How about simple casting Torrud posted a safe-cast approach in C# which is the best way to do casting (You don't need try catch for when the casting is illegal). Exterminator's way will work, but is not safe. exterminator July 18th, 2005, 09:21 AM Oh..we can for sure argue about it whole night long. But it surely depends upon your style of doing things. You would get a null when the casting fails using the 'as' operator but sometimes that would be somthing that would sound like and exception rather than null checking. You would better check null if there are any items in your arraylist or has it been fully built up and handle the exception when you feel that there has something happened that made your object not hold something of an ArrayList reference type. Basically, if you get a null you wont be able to guess as to what is the reason? Had my arraylist stored in object Obj not properly initialized or is it some incompatible type. Its usually better to keep different kind of exceptions/errors separetely handled as you might need that information. But anyways, it all depends on the logic you are trying to follow. Also, the 'as' operator works only for reference types I guess. Correct me If I am wrong. You might wish to look at this article (http://www.aspheute.com/english/20001019.asp) to get a better feel about the is-operator and the as-operator. I would vote for using the direct casting as it would help you keep the invalid object error away from the incompatible object error. Cheers. :thumb: Norfy July 19th, 2005, 04:49 AM I would go with a simple direct cast if I knew that my object was supposed to be an ArrayList. Exterminator covers the reasoning well. In this case I would probably use IList instead. If you decide to change the ArrayList to something else eg. an array then nothing else would need to change. ie. Better to decouple your classes via interfaces. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |