Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Casting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TrintiyPete - 18 Aug 2005 10:48 GMT
Hi All,

I have a piece of code where by I create an arraylist containing elements of
class type ActivityDetail, one of the routines used is generic and returns an
object array, but I can not cast back to ActivityDetail Array see snipet below

ArrayList arrayl = new ArrayList();
ActivityDetail det = new ActivityDetail();
arrayl.Add(det);
object[] obj = (object[]) arrayl.ToArray(typeof(object));
ActivityDetail[] detarr = (ActivityDetail[])obj;

Yet as all objects inherit object I thought this would be OK and I'm sure I
have done similar in the past. I just get invalid cast - any help or
explaination would be appreciated.

TrinityPete
Mattias Sjögren - 18 Aug 2005 11:42 GMT
>object[] obj = (object[]) arrayl.ToArray(typeof(object));

It should work if you change this to

object[] obj = (object[]) arrayl.ToArray(typeof(ActivityDetail));

>Yet as all objects inherit object I thought this would be OK

It's the type of the array itself (object[] vs ActivityDetail[]) that
matters, not what kind of objects that are actually referenced.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

TrinityPete - 18 Aug 2005 12:06 GMT
Thanks Mattias,

I was sure I had used similar methodology before and couldn't work out why
it wasn't working as expected in this instance. I now see the
ArrayList.ToArray was using the incorrect class.

The thing that threw me was in the dubugger looking at an element in the
object array still had an underlying Sysyem Type of Activity Detail therefore
I believed the cast was valid.

Thanks for your input.
Pete.

> >object[] obj = (object[]) arrayl.ToArray(typeof(object));
>
[quoted text clipped - 8 lines]
>
> Mattias

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.