2
    Class Person
    {
        string Name
        int yesno
        int Change
        List<Cars> Personcars;
        houses Personhouses
    }

Person user1 = new Person()
Person user2 = new Person()

user1.Name = "userName"
user2.Name ="";

user2.cars[0] = new car("Mazda");
user1.cars[0] = new car("BMW");

i want to merge the objects so that user2 will take the name and the car from user1

user2 will have this values

user2.Name will be userName user2.cars will hold the Mazda and the Bmw

thanks !

3
  • But what happens if the two merged Person s both have a name? And more worryingly if this is modelling people; how do you plan on merging the actual people? Commented Sep 21, 2011 at 17:53
  • That code won't even compile in it's current state. I think you need to work on this question. Commented Sep 21, 2011 at 17:53
  • All of the variables in your Person class are private. This can't be your actual code. Commented Sep 21, 2011 at 17:54

2 Answers 2

3
user2.Name = user1.Name;
user2.Personcars.AddRange(user1.Personcars);

You could add this as a method on the class itself:

public class Person
{
    List<Cars> _personcars;

    public string Name { get; set; }
    // what the hell is a yesno int? If it's 1 or 0 then just use a bool
    public int yesno { get; set; }
    public int Change { get; set; }
    public List<Cars> Personcars 
    {
        get
        {
            return _personcars ?? (_personCars = new List<Cars>());
        }
        set { _personcars = value; }
    }
    public Houses Personhouses { get; set; }

    public void Merge(Person person)
    {
        Name = person.Name;
        Personcars.AddRange(person.Personcars);
    }
}

Which will allow you to write something like this:

user2.Merge(user1);
Sign up to request clarification or add additional context in comments.

5 Comments

+1 '// what the hell is a yesno int? If it's 1 or 0 then just use a bool '
why even the need for int yesno?! If it's not a person, then why is it in the Person class?
@Code Monkey: Are you giving hunter a hard time about int yesno? It wasn't his design decision; he just carried it forward from the question (with editorial comment, at that).
@phoog: No I am not. Simmer down fido.
My level of yesno is probably around 49. I wish I could boil all people down, figuratively, into a Name, a collection of Cars, a Houses, plural, and with 2 seemingly meaningless integers.
3

Try this extension methods

 public void Merge(this Person _person, Person source)
 {
     _person.Name = source.Name;
     if(_person.Cars !=null)
     {
        _person.Cars.AddRang(source.Cars);
     }
     else
     {
        _person.Cars = source.Cars;
     }
 }

1 Comment

Extension method is kinda unnecessary in this situation. You can just add it as a public method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.