Chapter 23. User-defined conversionscasts


Chapter 23. User -defined conversions/casts

Here is another C# feature which adds to convenience for C# developers but which can be imitated using normal methods .

A user-defined conversion is a custom-made cast from one class to another. So far, type casting has been performed only if the object being cast is a subclass of the other. User-defined conversions allow you to perform casting of one type into an unrelated type. It also gives you the choice of what you want to do when performing the cast.

Sounds difficult? Study this example first.

 1: class Inch{  2:   public float InchValue;  3:  4:   public Inch (float value){ // constructor  5:     InchValue = value;  6:   }  7: }  8:  9: class Cm { 10:   public float CmValue; 11: 12:   public Cm (float value){ // constructor 13:     CmValue = value; 14:   } 15: } 

There are two classes “ one to represent inches, and the other to represent centimeters. [1] Of course, since they are not directly related in the class hierarchy, an attempt to explicitly cast an Inch object to a Cm type will result in a compilation error which reads: ' Cannot convert type Inch to Cm '. The same goes for casting a Cm object to an Inch type.

[1] This is just an example for demonstration purposes. You don't usually write public fields in classes unless there is a really good reason.

You can write a method in Cm to take in a parameter of type Inch and which returns a Cm object:

 public static explicit operator Cm (Inch toConvert) 

You can also write a method to take in a float value and return a Cm object:

 public static explicit operator Cm (float toConvert) 

Here is the rewritten class with the two methods added in.

 1: class Cm {  2:   public float CmValue;  3:  4:   public Cm (float value){  5:     CmValue = value;  6:   }  7:  8:  public static explicit operator Cm (float toConvert){  9:  Cm centi  =  new Cm (toConvert);  10:  return centi;  11:  }  12: 13:  public static explicit operator Cm (Inch toConvert){  14:  Cm centi  =  new Cm (toConvert.InchValue * 2.54f);  15:  return centi;  16:  }  17: } 

With these two methods, you can now cast a float type into a Cm type, or an Inch type to a Cm type! When performing the cast, the special method in lines 8 “ 11 (for a cast from float to Cm ) or that in lines 13 “ 16 (for a cast from Inch to Cm ) executes.

Examine TestClass , which contains a Main method below:

 18: class TestClass{ 19:   public static void Main(){ 20:     Inch inch = new Inch(3); 21:  Cm cm1  =  (Cm)inch; // cast an Inch to Cm  22:     Console.WriteLine(cm1.CmValue); 23: 24:  Cm cm2  =  (Cm)15.5f; // cast a float to Cm  25:     Console.WriteLine(cm2.CmValue); 26:   } 27: } 

Output:

 c:\expt>test 7.62 15.5 

When line 21 is reached, the conversion method public static explicit operator Cm (Inch toConvert) (line 13) is invoked with inch being passed in as the method parameter. The conversion method returns a Cm object (encapsulating the correct CmValue as calculated in that method), which is then assigned to cm1 .

Similarly, when line 24 is reached the conversion method on line 18 is invoked. This time the method doesn't contain any special business logic, but simply creates a new Cm object with CmValue assigned the same value as the passed in parameter, and returns the Cm object.

So far, it should be quite clear what user-defined casting is about. You write a special method which is automatically invoked when your cast happens.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net