Daniel Arroyo
Daniel Arroyo

Reputation: 83

Populate inherited members in derivated object using method made for base class in C#

Hey guys I want to achieve something like this

class Program
{
    static void Main(string[] args)
    {

        Responsible responsible = new Responsible()
        {
            //I want here to populate with PopulatePerson the base members
            Phone = "93827382",
            Company = "Google"
        };
    }

    public Person PopulatePerson(string pName, string pLastName)
    {
        Person person = new Person();
        person.Name = pName;
        person.LastName = pLastName;
        return person;
    }
}
public class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }
}



public class Responsible : Person
{

    public string Phone { get; set; }
    public string Company { get; set; }
}

The case is more complex with database queries and stuff but basically this is what I need

I could use a member called Person in Responsible and do Person = PopulatePerson("Dan", "Johns") but since I'm inheriting I find it kinda redundant

Upvotes: 1

Views: 176

Answers (2)

Silas
Silas

Reputation: 1150

Not sure if it makes sense your real scenario but you could change PopulatePerson to accept a Person object as an input parameter instead of internally creating a new one. Then you could pass your new Responsible object to it and afterwards set Phone and Company.

Edit: Like this

class Program
{
    static void Main(string[] args)
    {

        Responsible responsible = new Responsible();
        PopulatePerson(responsible, "first", "last");
        responsible.Phone = "93827382";
        responsible.Company = "Google";

    }

    public static void PopulatePerson(Person person, string pName, string pLastName)
    {
        person.Name = pName;
        person.LastName = pLastName;
    }
}
public class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }
}



public class Responsible : Person
{

    public string Phone { get; set; }
    public string Company { get; set; }
}

Upvotes: 0

Glenn Ferrie
Glenn Ferrie

Reputation: 10390

What about something like this. I created a generic (static) factory method for Person that is reusable across all types that inherit from Person.

class Program
{
    static void Main(string[] args)
    {

        //Responsible responsible = new Responsible()
        //{
        //    //I want here to populate with PopulatePerson the base members
        //    Phone = "93827382",
        //    Company = "Google"
        //};

        var responsible = Responsible.Populate("Glenn", "Fake", "93827382", "Google");
        //responsible
    }

    // NO LONGER NEEDED
    // ============================
    //public Person PopulatePerson(string pName, string pLastName)
    //{
    //    Person person = new Person();
    //    person.Name = pName;
    //    person.LastName = pLastName;
    //    return person;
    //}
}
public class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }

    public static TPerson Populate<TPerson>(string name, string lastname) where TPerson : Person, new()
    {
        TPerson person = new TPerson();
        person.Name = name;
        person.LastName = lastname;
        return person;
    }
}



public class Responsible : Person
{
    public static Responsible Populate(string name, string lastname, string phone, string company)
    {
        var p = Responsible.Populate<Responsible>(name, lastname);
        p.Phone = phone;
        p.Company = company;
        return p;
    }

    public string Phone { get; set; }
    public string Company { get; set; }
}

Upvotes: 1

Related Questions