logo       

Choosing A Webhost:
A web hosting service is a type of Internet hosting service that allows individuals and organizations to provide their own website accessible via the World Wide Web. Web hosts are companies that provide space on a server they own for use by their clients as well as providing Internet connectivity, typically in a data center. Web hosts can also provide data center space and connectivity to the Internet for servers they do not own to be located in their data center, called colocation. more...

Re: Casting question: msg#00185

windows.devel.dotnet.cx

Subject: Re: Casting question

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>
Google Custom Search

Recently Viewed:
qnx.openqnx.dev...    gcc.libstdc++.c...    solaris.opensol...    information-ret...    misc.misterhous...    web.catalyst.ge...    apache.webservi...    redhat.release....    hardware.lirc/2...    kernel.autofs/2...    technology.sust...    linux.vdr/2003-...    editors.lyx.gen...    org.user-groups...    netbsd.devel.pk...    xdg.devel/2004-...    version-control...    jakarta.slide.d...    debian.packages...    creativecommons...    ports.ppc.embed...    bug-tracking.bu...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive 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

Navigation