Converting object of a class to of another one

93,178

Solution 1

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

Solution 2

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>();

Solution 3

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;

Solution 4

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;

Solution 5

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>();
Share:
93,178

Related videos on Youtube

fhnaseer
Author by

fhnaseer

C#, WPF, MVVM, Entity Framework, WCF, TDD, Unit-Testing, Agile, Scrum,

Updated on July 09, 2022

Comments

  • fhnaseer
    fhnaseer almost 2 years

    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.

  • fhnaseer
    fhnaseer over 10 years
    It can be a solution, but code is being used in many places, so this solution will require much effort,
  • Matthew Watson
    Matthew Watson over 10 years
    You need to cast the doubles to floats to avoid a compile error.
  • letiagoalves
    letiagoalves over 10 years
    @MatthewWatson yap. Good eye. Thanks
  • fhnaseer
    fhnaseer over 10 years
    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
    fhnaseer over 10 years
    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?
  • letiagoalves
    letiagoalves over 10 years
    @FaisalHafeez you can try: FloatClass[] converted = Array.ConvertAll(array, s => (FloatClass) s); for simple arrays or see this implementation for 2D arrays: stackoverflow.com/a/6065947/1873446
  • fhnaseer
    fhnaseer over 10 years
    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?
  • Tim Schmelter
    Tim Schmelter over 10 years
    @letiagoalves: You can even omit the cast operator if you have an implicit cast: FloatClass fc = doubleObject;
  • letiagoalves
    letiagoalves over 10 years
    @TimSchmelter I know that but I prefer to do so. I don't mind the extra characters because it is more readable to me :)
  • Tigran
    Tigran over 10 years
    @FaisalHafeez: you can, for example, add an ExtensionMethds for your array class.
  • Servy
    Servy about 10 years
    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.
  • Levi Fuller
    Levi Fuller over 8 years
    CTRL+F -> "FloatClass" -> V (Expand) -> "GenericClass<float>" -> Replace All. Done :)
  • user57129
    user57129 over 6 years
    of course, because when you conver it, probably would be very salty, that's why you need to desalinize it after, jk
  • dading84
    dading84 almost 2 years
    I don't think this is the right way to convert objects