<< Chapter < Page Chapter >> Page >

Assignments of references, other than those listed above, require the use of a cast to change the type of the reference.

It is not always possible to perform a successful cast to convert the type of a reference. Whether or not a cast can be successfully performed depends on therelationship of the classes involved in the class hierarchy.

A reference to any object can be assigned to a reference variable of the type Object , because the Object class is a superclass of every other class.

When we cast a reference along the class hierarchy in a direction away from the root class Object toward the leaves, we often refer to it as a downcast .

Whether or not a method can be called on a reference to an object depends on the current type of the reference and the location in the class hierarchy wherethe method is defined. In order to use a reference of a class type to call a method, the method must be defined in or inherited into that class.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Xna0112-Type Conversion, Casting, and Assignment Compatibility
  • File: Xna0112.htm
  • Published: 02/27/14
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Complete program listing

A complete listing of the C# program discussed in this module is provided in Listing 11 .

Listing 11 . Project Polymorph02.

/*Project Polymorph02 Copyright 2002, R.G.BaldwinThis program illustrates downcasting Program output is:m in class B *********************************************************/using System; class A {//this class is empty }//end class A//======================================================// class B : A {public void m() { Console.WriteLine("m in class B");}//end method m() }//end class B//======================================================// class C {//this class is empty }//end class C//======================================================// public class Polymorph02 {public static void Main() { Object var = new B();//Following will not compile //var.m();//Following will not compile //((A)var).m();//Following will compile and run ((B)var).m();//Following will compile and run B v1 = (B)var;//Following will not execute. // Causes a runtime exception.//C v2 = (C)var; //Following will not compile//C v3 = (B)var; //Pause until user presses any key.Console.ReadKey(); }//end Main}//end class Polymorph02 //======================================================/

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Xna game studio' conversation and receive update notifications?

Ask