<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-14389562</id><updated>2012-01-18T20:20:16.123-08:00</updated><title type='text'>Learning C#</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://learningcsharpisfun.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14389562/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://learningcsharpisfun.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>abnegator</name><uri>http://www.blogger.com/profile/09139822935584399971</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-14389562.post-112185149169844129</id><published>2005-07-20T01:31:00.000-07:00</published><updated>2005-07-20T02:24:51.706-07:00</updated><title type='text'>Casting Object types to their relevant types</title><content type='html'>&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;This was something that I came across on codeguru. A guy had some issues while casting his object (base Object class' reference type). &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;Here's the question -&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;"How can we convert objects to arraylists ?" &lt;/span&gt;&lt;span style="color:#3333ff;"&gt;(kind of a little specific to ArrayLists but could be any types in general that is derived from the base class "Object".)&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;Here's the answer :&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;"Simple casting. How to do that? Its easy!"&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;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:&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;[CODE]&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;ArrayList myArrayList = (ArrayList) obj;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;where obj is retrieved from Session by something like:&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;[CODE]&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;//put into session&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;Session["ArrayList"] = someArrayList;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;//retrieve from session into an Object obj;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;obj = Session["ArrayList"];&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;//and then back into our ArrayList type as mentioned above&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;ArrayList myArrayList = (ArrayList) obj;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;You can also use the 'as' operator (safe-cast). (this was suggested by others):&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;[CODE]&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;//if obj is your ArrayList-object&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;ArrayList myList = obj as ArrayList;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;if (null != myList){ &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt; //do something with your list&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#cc6600;"&gt;}&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;Oh..we can for sure argue about the better way 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 something that would sound like an exception rather than a null check (won't it?). &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;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? (or would you ??) &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;Had my arraylist stored in object Obj not properly initialized or is it some incompatible type related issue? So, you would agree that 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. &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;You might wish to look at this article -&lt;/span&gt;&lt;/strong&gt;&lt;a href="http://www.aspheute.com/english/20001019.asp"&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt; http://www.aspheute.com/english/20001019.asp&lt;/span&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style="color:#3333ff;"&gt;  - 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 seperated away from the incompatible object exception. Cheers. &lt;/span&gt;&lt;/strong&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/14389562-112185149169844129?l=learningcsharpisfun.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learningcsharpisfun.blogspot.com/feeds/112185149169844129/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=14389562&amp;postID=112185149169844129&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/14389562/posts/default/112185149169844129'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14389562/posts/default/112185149169844129'/><link rel='alternate' type='text/html' href='http://learningcsharpisfun.blogspot.com/2005/07/casting-object-types-to-their-relevant.html' title='Casting Object types to their relevant types'/><author><name>abnegator</name><uri>http://www.blogger.com/profile/09139822935584399971</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-14389562.post-112109421927428911</id><published>2005-07-11T08:02:00.000-07:00</published><updated>2005-07-16T03:46:04.553-07:00</updated><title type='text'>Learning C#</title><content type='html'>&lt;strong&gt;This blog would be a collection of some quality issues related to the C# Programming Language. A compilation of my activities on Codeguru. Hope this comes out to be of help to you people and of course me.&lt;br /&gt;&lt;br /&gt;Cheers to my 3rd blog.&lt;br /&gt;[ http://abnegator.blogspot.com ]&lt;/strong&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/14389562-112109421927428911?l=learningcsharpisfun.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learningcsharpisfun.blogspot.com/feeds/112109421927428911/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=14389562&amp;postID=112109421927428911&amp;isPopup=true' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/14389562/posts/default/112109421927428911'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14389562/posts/default/112109421927428911'/><link rel='alternate' type='text/html' href='http://learningcsharpisfun.blogspot.com/2005/07/learning-c.html' title='Learning C#'/><author><name>abnegator</name><uri>http://www.blogger.com/profile/09139822935584399971</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry></feed>
