<< Chapter < Page Chapter >> Page >

Listing 9 . Cannot be assigned to C.

//Following will not execute. // Causes a runtime exception.//C v2 = (C)var;

The code in Listing 9 attempts to cast the reference to type C and assign it to a reference variable of type C .

A runtime exception

Although the program will compile, it won't execute. An attempt to execute the statement in Listing 9 results in an exception at runtime.

As a result, it was necessary to convert the statement into a comment in order to execute the program.

Another failed attempt

Similarly, an attempt to cast the reference to type B and assign it to a reference variable of type C , as shown in Listing 10 , won't compile.

Listing 10 . Another failed attempt.

//Following will not compile //C v3 = (B)var;//Pause until user presses any key. Console.ReadKey();}//end Main }//end class Polymorph02

The problem here is that the class C is not a superclass of the class named B . Therefore, a reference of type B is not assignment-compatible with a reference variable of type C .

Again, it was necessary to convert the statement to a comment in order to compile the program.

The end of the program

Listing 10 signals the end of the Main method, the end of the class, and the end of the program.

Run the program

I encourage you to copy the code from Listing 11 . Use that code to create a C# console project. Compile and run the project. Experiment with the code,making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Run my program

Click here to download a zip file containing my version of the program. Extract the folder named Polymoeph02 from the zip file and save it somewhere on your disk. Start Visual C# 2010 Express and select Open Project... from the File menu. Navigate to the project folder and select the file with the extension of .sln . This should cause the project to open and be ready to run or debug as described in the earlier module titled Getting Started .

Summary

This module discusses type conversion for primitive and reference types.

A value of a particular type may be assignment-compatible with variables of other types.

If the type of a value is not assignment-compatible with a variable of a given type, it may be possible to perform a cast on the value to change its typeand assign it to the variable as the new type. For primitive types, this will often result in the loss of information.

In general, numeric values of primitive types can be assigned to any variable whose type represents a numeric range that is as wide as or wider than the rangeof the value's type. (Values of type bool can only be assigned to variables of type bool .)

With respect to reference types, the reference to an object instantiated from a given class can be assigned to any of the following without the use of a cast:

  • Any reference variable whose type is the same as the class from which the object was instantiated.
  • Any reference variable whose type is a superclass of the class from which the object was instantiated.
  • Any reference variable whose type is an interface that is implemented by the class from which the object was instantiated.
  • Any reference variable whose type is an interface that is implemented by a superclass of the class from which the object was instantiated.
  • A couple of other cases involving interfaces that extend other interfaces.

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