Converting object of a class to of another one

fhnaseer

I have two classes which have are nearly equal except the data types stored in them. One class contains all double values while other contains all float values.

class DoubleClass
{
    double X;
    double Y;
    double Z;
}

class FloatClass
{
    float X;
    float Y;
    float Z;
}

Now I have a point of DoubleClass which I want to convert to FloatClass.

var doubleObject = new DoubleClass();

var convertedObject = (FloatClass)doubleObject; // TODO: This

One simple way is to make a method which creates a new FloatClass object, fills all values and return it. Is there any other efficient way to do this.

letiagoalves

Use a conversion operator:

public static explicit operator FloatClass (DoubleClass c) {
   FloatCass fc = new FloatClass();
   fc.X = (float) c.X;
   fc.Y = (float) c.Y;
   fc.Z = (float) c.Z;
   return fc;
}

And then just use it:

var convertedObject = (FloatClass) doubleObject;

Edit

I changed the operator to explicit instead of implicit since I was using a FloatClass cast in the example. I prefer to use explicit over implicit so it forces me to confirm what type the object will be converted to (to me it means less distraction errors + readability).

However, you can use implicit conversion and then you would just need to do:

var convertedObject = doubleObject;

Reference

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting list of one class to list of another class?

From Dev

ruby inheritance. Converting one object into another

From Dev

ruby inheritance. Converting one object into another

From Dev

Using one class object in another class PYTHON

From Dev

Assigning one object to another of the same class

From Dev

convert one class object into another using stream

From Dev

Assigning one object to another of the same class

From Dev

Assign the pointer of one class object to another class object c++

From Dev

Method to accept one class object or another class object

From Dev

Adding an object of one class to an object of another class and checking for duplicates in Python

From Dev

assign one class object to another class object in c++

From Dev

converting json object in to another object

From Dev

How to create a class that one of its object is in the type of another class in VB?

From Dev

How to create a class that one of its object is in the type of another class in VB?

From Dev

How to transfer object from one class to another class - JAVA

From Dev

How to inject one object to another class object in spring?

From Dev

Converting custom class object into an Object[]

From Dev

Copy data from one object from one class to another boject in another class in Android

From Dev

Converting from one struct to another

From Dev

Converting one data structure to another

From Dev

Converting one of the value of json to object

From Dev

Converting Object of SuperClass to Sub Class

From Dev

Converting custom class object into NSData

From Dev

Converting custom class to object and back

From Dev

Converting Object of SuperClass to Sub Class

From Dev

Converting Class object to Json for WebService

From Dev

converting class name and passing to another class

From Dev

Mule ESB: Converting JSON Object to another object

From Dev

How to pass itcl object of one class to object of another class and use the functions of the passed objects?

Related Related

  1. 1

    Converting list of one class to list of another class?

  2. 2

    ruby inheritance. Converting one object into another

  3. 3

    ruby inheritance. Converting one object into another

  4. 4

    Using one class object in another class PYTHON

  5. 5

    Assigning one object to another of the same class

  6. 6

    convert one class object into another using stream

  7. 7

    Assigning one object to another of the same class

  8. 8

    Assign the pointer of one class object to another class object c++

  9. 9

    Method to accept one class object or another class object

  10. 10

    Adding an object of one class to an object of another class and checking for duplicates in Python

  11. 11

    assign one class object to another class object in c++

  12. 12

    converting json object in to another object

  13. 13

    How to create a class that one of its object is in the type of another class in VB?

  14. 14

    How to create a class that one of its object is in the type of another class in VB?

  15. 15

    How to transfer object from one class to another class - JAVA

  16. 16

    How to inject one object to another class object in spring?

  17. 17

    Converting custom class object into an Object[]

  18. 18

    Copy data from one object from one class to another boject in another class in Android

  19. 19

    Converting from one struct to another

  20. 20

    Converting one data structure to another

  21. 21

    Converting one of the value of json to object

  22. 22

    Converting Object of SuperClass to Sub Class

  23. 23

    Converting custom class object into NSData

  24. 24

    Converting custom class to object and back

  25. 25

    Converting Object of SuperClass to Sub Class

  26. 26

    Converting Class object to Json for WebService

  27. 27

    converting class name and passing to another class

  28. 28

    Mule ESB: Converting JSON Object to another object

  29. 29

    How to pass itcl object of one class to object of another class and use the functions of the passed objects?

HotTag

Archive