Jack Tyler
Jack Tyler

Reputation: 529

How to a assign data from one object another object of a different type that inherits from the original object?

I have Two Classes:

class object {
    int id{ get; set; }
    string name { get; set; }
    string description { get; set; }
}

and

class extendedObject : object {
        int quantity { get; set; }
        string moreDetail{ get; set; }
    }

I want to assign the properties of object to the extended object.

for example extendedObject = (extendedObject)object;

Upvotes: 0

Views: 330

Answers (3)

Marco Salerno
Marco Salerno

Reputation: 5203

By language design, you can't cast a BaseObject to ExtendedObject.

You can't even define and implicit or explicit operator between them.

This is a possible workaround:

class Program
{
    static void Main(string[] args)
    {
        BaseObject baseObject = new BaseObject()
        {
           ID = 0,
           Name = "TEST",
           Description = "TEST"
        };
        ExtendedObject extendedObject = new ExtendedObject(baseObject);
    }
}

public class BaseObject
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class ExtendedObject : BaseObject
{
    public ExtendedObject(BaseObject baseObject)
    {
        this.ID = baseObject.ID;
        this.Name = baseObject.Name;
        this.Description = baseObject.Description;
    }
    public int Quantity { get; set; }
    public string MoreDetails { get; set; }
}

Upvotes: 1

asd
asd

Reputation: 904

The direct cast not is posible. Your must declare the class as derived class:

public class Program
{
    public static void Main()
    {
        BaseClass c = new ExtendedClass() { id= 2, name = "Jhon"};

        ExtendedClass extended = c as ExtendedClass;        
        Console.WriteLine(extended.name);
    }
}

public class BaseClass 
{
    public int id{ get; set; }
    public string name { get; set; }
    public string description { get; set; }
}

public class ExtendedClass : BaseClass 
{
    public int quantity { get; set; }
    public string moreDetail{ get; set; }
}

Upvotes: 0

Ilya Chernomordik
Ilya Chernomordik

Reputation: 30185

From your example it looks like you are trying to assign a variable, not properties. That is not possible since inherited class cannot contain a variable of base class.

If you want to have a separate object of type extendedObject and copy data from object, then I am afraid you'll have to write the code yourself. Either use a copy constructor or a separate method.

E.g.

class extendedObject : object 
{
        int quantity { get; set; }
        string moreDetail{ get; set; }

     public extendedObject(object myObject)
     {
         id = myObject.id.
         /// etc.
     }

}

P.S. There are all kinds of mappers (special libraries) around like e.g. AutoMapper, etc. that can make this task easier for you, but I would not recommend that, it's just more expensive in the long run in my experience.

Upvotes: 1

Related Questions