0

I have a scenario wherein I have an (objA) of (Class A) and (objB) of (Class B). The member variables in Class A and Class B do not match. I need to assign data from objA to objB. I searched around and found solutions which are related to mapping objA into objB where member variables are the same. But I havent found anything when members in Class A and class B are different.

Below are 2 sample classes (class A and class B) to give an idea.

public class Class A
{
    private ABCHeaderType ABCHeaderField;
    private string title;
    private string date;
    private string id;
    private string location;
    private string status;
    private string hashNumber;      
    .....skipped getter setters     
}   
public class ABCHeaderType { 
    private string version;    
    private string outputTypeField;    
    private int langCd;       
    private string dateTime;    
    private string hashField;
        .....skipped getter setters
}

public class Class B
{
    public Input input { get; set; }

}
public class Input
{
    public Output output{ get; set; }
}
public class Output
{
    public string Title { get; set; }
    public string Date { get; set; }
    public string id { get; set; }
    public string location { get; set; }
    public string Status { get; set; }
    public string laterDate { get; set; }
    public string hashNumber { get; set; }
    public Info info { get; set; }
}
public class Info
{
    public string name { get; set; }
    public string ht { get; set; }
    public string type { get; set; }
    public string res { get; set; }
    public string wd { get; set; }
    public string ornt { get; set; }
}

Right now I am copying objA into objB using a converter class and copying over using the code snippet below

objB.input.output.Title = objA.Title;
objB.input.output.Date = objA.Date;
objB.input.output.id = objA.id;
objB.input.output.location = objA.location;
objB.input.output.Status = objA.Status;
objB.input.output.hashNumber = objA.hashNumber;

objB.input.output.inf.type = objA.ABCHeaderField.outputTypeField

Thanks...

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • if the property names are identical from both class then there is a chance to do that! with the help of this:https://stackoverflow.com/a/4144817/7124761 otherwise I think its not possible! – Prashant Pimpale Nov 22 '19 at 05:58
  • have a look at automapper. automapper is a library aimed for this purpose. – Arjun Vachhani Nov 22 '19 at 06:01
  • How are the two classes related, if their members are different? Maybe they have a common ancestor or some of the properties can be encoded into a common Interface? | If your current code works, you should keep using it. The only other way I can think off uses Reflection. Reflection is a good fallback - but should never be used as a effective/primary solution. Reflection is what you use if you do not (yet) have a Converter code like you showed. – Christopher Nov 22 '19 at 06:19
  • @Christopher, the 2 classes are related in a way that - consider objA is an incoming object from one system and objB is an object to send out to a different other system. But objB needs the data from objA – user12414374 Nov 22 '19 at 13:53

2 Answers2

0

You can create an interface BaseObject with base fields that can be implemented by classes A and Output:

public interface BaseObject
{
    string Title { get; set; }
    string Date { get; set; }
    string id { get; set; }
    string location { get; set; }
    string Status { get; set; }
    string laterDate { get; set; }
    string hashNumber { get; set; }
    Info info { get; set; }
}
public class A : BaseObject
{
    public string Title { get; set; }
    public string Date { get; set; }
    public string id { get; set; }
    public string location { get; set; }
    public string Status { get; set; }
    public string laterDate { get; set; }
    public string hashNumber { get; set; }
    public Info info { get; set; }
  // your code....
}
public class B
{
    public Input Input { get; set; }
}
public class Input
{
    public BaseObject Output{ get; set; }
}

public class Output : BaseObject
{
    public string Title { get; set; }
    public string Date { get; set; }
    public string id { get; set; }
    public string location { get; set; }
    public string Status { get; set; }
    public string laterDate { get; set; }
    public string hashNumber { get; set; }
    public Info info { get; set; }
}

And then you can do it:

var objB = new B
{
     Input = new Input
     {
        Output = new A()
      }
};

If obejct was initialized and was initialized field Input:

objB.Input.Output = objA;
GDI89
  • 54
  • 3
0

From the comments:

The 2 classes are related in a way that - consider objA is an incoming object from one system and objB is an object to send out to a different other system. But objB needs the data from objA

How about a class "shared Data"?. Something that has all the fields both sides got in common. Depending on the case, a readonly struct might be better - otherwise you may have to clone.

public readonly struct SharedData{
    public readonly string Title;
    public readonly string Date;
    public readonly string id;
    public readonly string location;
    public readonly string Status;
    public readonly string laterDate;
    public readonly string hashNumber;
}

The MVVM pattern deals a lot with presentation. Indeed that is most of what the View Model - half those letters - are there for. In MVVM you need properties with change notification, often ones that accept raw strings too. The view usually does not have those - and can not be modified to get those.

"If you can not modify it, wrap it into something you can modify". Most View classes can not be modified or inherited. Because they come from some outside source, or their current behavior is needed. So you usually have to write ViewModel classes for the primary purpose of wrapping around those View classes. (There are other advantages to making VM classes, but those tend to be MVVM/XAML specific).

Christopher
  • 9,634
  • 2
  • 17
  • 31