<< 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-

Questions & Answers

the art of managing the production, distribution and consumption.
Satangthem Reply
what is economics
Khawar Reply
okk
damfash
marginal utility is the additional satisfaction one derives from consuming additional unit of a good or service.
Fred
It's the allocation of scarce resources.
Fred
marginal utility please?
Dishan
marginal utility is the additional satisfaction one derives from consuming additional unit of a good or service.
Fred
I know the definition, but I don't understand its meaning.
Dishan
what is the must definition of economic please?
Nurudeen
demand lfs
Alpha
Economics is derived from the word Oikonomia which means management of household things. Thus, Economics is a study of household things with the constrains of allocating scare resources.
Dishan
what is Open Market Operation
Adu Reply
dominating middlemen men activities circumstances
Christy Reply
what Equilibrium price
Adji Reply
what is gap
mirwais
who is good with the indifference curve
Dexter
What is diseconomic
Alixe Reply
what are the types of goods
WARIDI
how can price determination be the central problem of micro economics
simon Reply
marginal cost formula
Nandu Reply
you should differentiate the total cost function in order to get marginal cost function then you can get marginal cost from it
boniphace
What about total cost
Foday
ok
Foday
how can price determination be the central problem if micro economics
simon
formula of cross elasticity of demand
Theresia Reply
what is ceteris paribus
Priyanka Reply
what is ceteris parabus
Priyanka
Ceteris paribus - Literally, "other things being equal"; usually used in economics to indicate that all variables except the ones specified are assumed not to change.
Abdullah
What is broker
scor
land is natural resources that is made by nature
scor
What is broker
scor
what is land
kafui
What is broker
scor
land is natural resources that is made by nature
scor
whats poppina nigga turn it up for a minute get it
amarsyaheed Reply
what is this?
Philo
am from nigeria@ pilo
Frank
am from nigeria@ pilo
Frank
so
owusu
what is production possibility frontier
owusu
it's a summary of opportunity cost depicted on a curve.
okhiria
please help me solve this question with the aid of appropriate diagrams explain how each of the following changes will affect the market price and quantity of bread 1. A
Manuela Reply
please l need past question about economics
Prosper Reply
ok let me know some of the questions please.
Effah
ok am not wit some if den nw buh by tommorow I shall get Dem
adepojurafiu
Hi guys can I get Adam Smith's WEALTH OF NATIONS fo sale?
Ukpen
hello I'm Babaisa alhaji Mustapha. I'm studying Economics in the university of Maiduguri
Babaisa
okay
Humaira
my name is faisal Yahaya. i studied economics at Kaduna state university before proceeding to West African union university benin republic for masters
Faisal
Hi guys..I am from Bangladesh..
Mannan
Wat d meaning of management
igwe Reply
disaster management cycle
Gogul Reply
cooperate social responsibility
igwe
Fedric Wilson Taylor also define management as the act of knowing what to do and seeing that it is done in the best and cheapest way
OLANIYI
Difference between extinct and extici spicies
Amanpreet Reply
Researchers demonstrated that the hippocampus functions in memory processing by creating lesions in the hippocampi of rats, which resulted in ________.
Mapo Reply
The formulation of new memories is sometimes called ________, and the process of bringing up old memories is called ________.
Mapo Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





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