|
|
Choosing A Webhost: |
Re: Casting question: msg#00179windows.devel.dotnet.cx
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. My solution doesn't do quite that, but instead allows you to specify the desired result type, a la ArrayList.ToArray(Type) (and incidentally, .Net Reflector rocks :) ) -------------- Original message -------------- > I still don't get what is the problem. If your A[] array only contains B > objects, then you can cast it to a B[] array. I tried it here and it worked: > > public static A[] Gimme(int i) > { > A[] a = new B[i]; > return a; > } > > public static void Test() > { > B[] b = (B[])Gimme(5); // invalid cast -> not anymore > } > > There is no need for a casting operator A[] to B[]. If in fact your real > life array has something else beside Bs (let's say that it also contains Cs, > where C is directly derived from A and not from B) you will bump into the > same issue because you will not be able to cast C objects to a B reference. > > -----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 3:09 PM > To: DOTNET-CX@xxxxxxxxxxxxxxxxxxx > Subject: Re: [DOTNET-CX] Casting question > > > right, caught that - but, it is correct. the function is really returning an > A[], and should, but each element in the array is a B. > > -------------- Original message -------------- > > > Rick, > > > > Maybe in real life you do create objects of derived type(s) (B, C, D or L) > > and store them in an array of refrences to the base type (A) but not in > the > > example you sent us. > > Change the code for your Gimme method like this: > > > > public static A[] Gimme(int i) > > { > > // instead of > > // A[] a = new A[i]; > > // have this > > A[] a = new B[i]; > > return a; > > } > > > > and you will not get the exception anymore. > > > > Cheers, > > Eddie > > > > -----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 2:33 PM > > To: DOTNET-CX@xxxxxxxxxxxxxxxxxxx > > Subject: Re: [DOTNET-CX] Casting question > > > > > > no.. > > > > in the real life example, there are multiple types that derive from A. In > > the scenario we are really working on, we know that the equivcalent of > > Gimme() always returns an array of B objects for this particular case -- > > although because under other circumstances it returns C, D, and L, the > > method signature for Gimme() remains > > > > public static A[] Gimme(int i) > > (ie, returns A[] so it can fit all its target cases) > > Assume that what comes back from Gimme() is an array of A, where every A > in > > the array is really a B. > > -------------- Original message -------------- > > > > > A is the base class and B is the derived one. You create an array of > base > > > objects and expect to cast them to derived ones. If you replace the > > classes > > > like this (which may have been your initial intention) everything is > fine: > > > > > > public class B { } > > > public class A : B { } > > > > > > HTH, > > > Eddie > > > > > > -----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 1:50 PM > > > To: DOTNET-CX@xxxxxxxxxxxxxxxxxxx > > > Subject: [DOTNET-CX] Casting question > > > > > > > > > I feel like this should be obvious, but, why is the cast in Test() > > invalid? > > > This compiles but throws an exception. (sorry about the format, comcast > > web > > > interface throwing things off) > > > > > > public class A { } > > > public class B : A { } > > > public class Tester > > > { > > > public static A[] Gimme(int i) > > > { > > > A[] a = new A[i]; > > > return a; > > > } > > > > > > public static void Test() > > > { > > > B[] b = (B[])Gimme(5); // invalid cast > > > } > > > } > > > > > > =================================== > > > 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 > > > > =================================== > > 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 =================================== 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, Jon Skeet |
| Previous by Thread: | Re: Casting question, Rick Taylor |
| Next by Thread: | Re: Casting question, Kenneth Lee |
| 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 |