23

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.

Darren
  • 68,902
  • 24
  • 138
  • 144
fhnaseer
  • 7,159
  • 16
  • 60
  • 112

8 Answers8

32

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
letiagoalves
  • 11,224
  • 4
  • 40
  • 66
6

Sounds like you could use generics here:

 public class GenericClass<T>
 {
    T X { get; set; }
    T Y { get; set; }
    T Z { get; set; }
 }

 GenericClass<float> floatClass = new GenericClass<float>();
 GenericClass<double> doubleClass = new GenericClass<double>();
Darren
  • 68,902
  • 24
  • 138
  • 144
5

You can use Conversion Operators to achieve this.

Fr example:

struct FloatClass
{
    public FloatClass(DoubleClass dClass) {
        //conversion...
    }
    ... 
    public static explicit operator FloatClass(DoubleClass dClass) 
    {
        FloatClassd = new FloatClass(dClass);  // explicit conversion

        return d;
    }
}


var convertedObject = (FloatClass)doubleObject;
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Now I want to convert a 2D array of DoubleClass to FloatClass, is it possible using LINQ? or I have to use for loops here? – fhnaseer Sep 13 '13 at 11:16
  • 1
    @FaisalHafeez: you can, for example, add an [ExtensionMethds](http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx) for your array class. – Tigran Sep 13 '13 at 11:40
2

You could add an implicit type conversion operator:

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

    public static implicit operator FloatClass(DoubleClass d)
    {
        return new FloatClass { X = (float)d.X, Y = (float)d.Y, Z = (float)d.Z };
    }
}

Now this works:

DoubleClass doubleObject = new DoubleClass();
FloatClass convertedObject = doubleObject;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Now I want to convert a 2D array of DoubleClass to FloatClass, is it possible using LINQ? or I have to use for loops here? – fhnaseer Sep 13 '13 at 11:13
1

Add a class for thease extention methods :

public static class ExtensionMethods
{
    public static T ToObject<T>(this Object fromObject)
    {
        return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(fromObject));
    }


    public static List<T> ToObjectList<T>(this Object fromObject)
    {
        return JsonConvert.DeserializeObject<List<T>>(JsonConvert.SerializeObject(fromObject));
    }
}

Use :

using YourExtentionMethodNamespace;

Class2 obj2 = obj1.ToObject<Class2>();
List<Class2> lst2 = _db.Blogs.ToList().ToObjectList<Class2>();
M Komaei
  • 7,006
  • 2
  • 28
  • 34
0

The simplest way to do this is by using serializer. Use Newtonsoft JSON serializer which works best.

using Newtonsoft.Json;

  private void Convert()
    {
        DoubleClass doubleClass = new DoubleClass {X = 123.123, Y = 321.321, Z = 111.111};
        var serializedoubleClass = JsonConvert.SerializeObject(doubleClass);
        var floatClass = JsonConvert.DeserializeObject(serializedoubleClass, typeof(FloatClass));
    }
Aamol
  • 1,149
  • 1
  • 15
  • 23
0

Best way for Convert

public static class Extention {
     public static string ConvertObjectToJson(this object ob)
     {
         return JsonConvert.SerializeObject(ob);
     }
}

For Usage

var doubleClass = new DoubleClass {
   x = 10,
   y = 20
};
var floatClass = JsonConvert.DeserializeObject<FloatClass>(doubleClass.ConvertObjectToJson());
-3

Best way is Serialize object and again desalinize it

  • 1
    The deserialization will fail because the structures are not equivalent in their storage mechanisms. Even if they were, and it could work, it would still be prohibitively expensive and a fairly poor practice to do this. – Servy Apr 24 '14 at 13:59
  • of course, because when you conver it, probably would be very salty, that's why you need to desalinize it after, jk – user57129 Jan 06 '18 at 22:59