|
|
Choosing A Webhost: |
Re: Casting question: msg#00183windows.devel.dotnet.cx
Discussion, Then you can't assume that each A[] is a B[]: A[] theAs = Gimme(5); B[] theBs = new B[5]; int counter = 0; foreach (A a in theAs) { // Copy only the B's theBs[counter] = a as B; counter++; } You'll probably want something a little different since you'll have nulls in the array where an A is not a B. Thanks, Shawn Wildermuth http://adoguy.com C# MVP, MCSD.NET, Author and Speaker -----Original Message----- From: Discussion relating to the specifics of the C# and Managed C++ languages [mailto:DOTNET-CX@xxxxxxxxxxxxxxxxxxx] On Behalf Of Rick Taylor Sent: Monday, September 26, 2005 8:13 PM To: DOTNET-CX@xxxxxxxxxxxxxxxxxxx Subject: Re: [DOTNET-CX] Casting question "why to you want to cast an A array into a B array if you know all the elements of the array are B class objects" Because for a given set of input, the function in the real code returns an array with all B elements. Under other conditions, it may return C, D, M, what have you -- all of which derive from A. But yes, a nice restating of the whys of the solution. -----Original Message----- From: Discussion relating to the specifics of the C# and Managed C++ languages [mailto:DOTNET-CX@xxxxxxxxxxxxxxxxxxx] On Behalf Of Kenneth Lee Sent: Monday, September 26, 2005 5:30 PM To: DOTNET-CX@xxxxxxxxxxxxxxxxxxx Subject: Re: [DOTNET-CX] Casting question In a message dated 9/26/2005 12:54:19 PM Pacific Standard Time, taylorrx@xxxxxxxxxxx writes: You're returning a B[] cast as an A[]. We need to return an A[] filled with derived type B (and then cast it to B[]). That is, create an A[] and then iterate through it and fill it with new Bs. If you return an A[] array, that array can't be cast to a B[] array even if you fill it with only B type objects. Your alternate solution returns a B[] array cast as an A[] array. That is why you can cast the returned A[] array back to B[]. You can cast the individual items to B because they contain B classes. You can't cast the A[] array back to B[] because it ISN'T a B[] array. When you specify "A[] a=new A[5]" you create a class A array in memory with five pointers to class A objects. All of those pointers point to the "null" address. You can't do "a[0]=new object();" because while A is an object, an object is not an A. You can do "a[0]=new B();" because B is an A and the first pointer in the A array is really a B object, so "B b=(B)a[0];" because the object in the first position of the A array is a B object. Filling all five pointers in the array with B objects does not change the array into a B array. By the way, I took your original code, modified only the B class and test code (that runs): public class B : A {public int x;} A[] a = Gimme(5); for (int i=0;i<5;i++) { a[i] = new B(); ((B)a[i]).x=i; } foreach (B bOb in a) { Console.WriteLine(bOb.x.ToString()); } Console.Read(); return; Of course the above foreach would blow up big time if you had a class E object in any of the A array slots, but why to you want to cast an A array into a B array if you know all the elements of the array are B class objects? =================================== This list is hosted by DevelopMentor. http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor. http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Casting question, Rick Taylor |
|---|---|
| Next by Date: | Re: Casting question, Rick Taylor |
| Previous by Thread: | Re: Casting question, Rick Taylor |
| Next by Thread: | Re: Casting question, Rick Taylor |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
Free MagazinesCisco NewsReceive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business. subscribe Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field. subscribe The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business. subscribe Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company. subscribe Total Telecom Total Telecom is "The Economist of the communications industry". subscribe |