|
|
Choosing A Webhost: |
Re: Casting question: msg#00185windows.devel.dotnet.cx
In a message dated 9/26/2005 5:12:44 PM Pacific Standard Time, taylorrx@xxxxxxxxxxx writes: 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. I asked why you wanted to recast the array because of the foreach loop: foreach (B bOb in a) "a" was defined as an A[] array, but I was able to treat it like it was a B[] array in my example code. True, if you want to index the a variables, they have to be explicitly cast as B objects to access the B functionality, so it isn't really a B[] array. Your workaround really created a B[] array and cast it as an A[] array when it was returned. You could just as easily returned an object[] array because B[] isa A[] isa object[]. You can't create a new "object" object and then cast it as an A because all the background information of the A type of object is missing. For the same reason, you can't create an A object and cast it as a B object. You can create a new B or C object and then cast it as an A or "object" because all the background information of the A or "object" type of object is included. You define the actual type of the object when you create an instance of the object. Say you add a class C that's an exact copy of my B class. A[] b = new B[5];-- >the b array is really a B class array. b[0] = new C();-- > compiles because the object is an A object and runs!? Ahh, no, the debugger points to the next line of code, but this is the line that really fails with a runtime error because the underlying type of array object is B, not A or C. (Declaring the array new A[5] runs fine. A C object can be cast to A but it can't be cast to B.) B b = new A();--> won't even compile type safe checking catches this error. B[] b = new A[5];--> won't even compile type safe checking catches this error. A a = new A(); C c = (C) a; -->compiles because it is possible for "a" to cast to type C, but generates a runtime error because this "a" instance won't support it. The array definitions work exactly the same way. Putting "a = new C();" between the previous two lines and it will not only compile, but it will run. Put "B b= (B) a;" after the prior three lines and it generates a runtime error. Are you beginning to see why you can't cast an A[] array to a B[] array, but you can cast a B[] array to an A[] variable and then cast that variable back to a B[] array? =================================== 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: Need fast Bitmap Compare Method, Marc Brooks |
| Previous by Thread: | Re: Casting question, Peter Golde |
| Next by Thread: | Mathmatical Question, Formula to multiply a number by smaller amounts?, Zeusbwr |
| 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 |