Method parameters and overload

203 Views Asked by At

Is it possible to do something like that:

class Program
{
    static void Main(string[] args)
    {
        Customer1 c1 = new Customer1();
        DoSomething(c1);

        Customer2 c2 = new Customer2();
        DoSomething(c2);
    }

    static void DoSomething<T>(T customer)
    {
        //... code here ...
        InitializeCustomer(customer); // <- error indeed :-(
        //... code here ...
    }

    static void InitializeCustomer(Customer1 c1)
    {
        c1.Reference = 1234;
        c1.Name = "John";
    }

    static void InitializeCustomer(Customer2 c2)
    {
        c2.Name = "Mary";
        c2.Town = "Tokyo";
    }
}

class Customer1
{
    public int Reference;
    public string Name;
}

class Customer2
{
    public string Name;
    public string Town;
}

I would like to avoid creating 2 "DoSomething" methods and also avoiding to copy the code twice with different method parameters. I thought to use an object as parameter as well but I need to cast after that... Can you advice me?

Thanks.

4

There are 4 best solutions below

0
On BEST ANSWER

Since Customer1 and Customer2 do not share a common interface, this is not possible.

However, you could rework this so they derive from a base class (or interface), and do their own initialization. This would be much cleaner, as well, since it allows each Customer to initialize itself, which keeps a cleaner separation of concerns.

For example:

class Program
{
    static void Main(string[] args)
    {
        Customer1 c1 = new Customer1();
        DoSomething(c1);

        Customer2 c2 = new Customer2();
        DoSomething(c2);
    }

    static void DoSomething<T>(T customer) where T : Customer
    {
        //... code here ...
        customer.Initialize();
        //... code here ...
    }
}

abstract class Customer
{
    public abstract void Initialize();

}
class Customer1 : Customer
{
    public int Reference;
    public string Name;

    public override void Initialize()
    {
        this.Reference = 1234;
        this.Name = "John";
    }
}

class Customer2 : Customer
{
    public string Name;
    public string Town;

    public override void Initialize()
    {
        this.Name = "Mary";
        this.Town = "Tokyo";
    }
}
1
On

Your Customer1 and Customer2 should either inherit from a common AbstractCustomer class or ICustomer interface.

This will allow you to use a single method that handles both and also eliminate the need for generics:

static void DoSomething(ICustomer customer)
{
    //... code here ...
    InitializeCustomer(customer); 
    //... code here ...
}

static void InitializeCustomer(ICustomer c)
{
    c.Reference = 1234;
    c.Name = "John";
}

As @Reed Copsey notes, this solution assumes that both types have the same members.

If you provide an identical initialization method for both (it terms of method signature), you can initialize them separately.

0
On

You need to have a separate class instance to do that.

A much better approach is to use inheritance, where once customer inherits from another or, better yet, both inherit from a common base. Then you can put an initialize routine just about anywhere by calling the appropriate method and the correct version will be called.

0
On

Maybe share the interface?

interface ICustomer
{
    void Initialize();
}

class Program
{

    static void Main(string[] args)
    {
        Customer1 c1 = new Customer1();
        DoSomething(c1);

        Customer2 c2 = new Customer2();
        DoSomething(c2);
    }

    static void DoSomething<T>(T customer) where T : ICustomer
    {
        customer.Initialize();
     }
}

class Customer1 : ICustomer
{
    public void Initialize()
    {
        Reference = 1234;
        Name = "John";
    }

    public int Reference;
    public string Name;
}

class Customer2 : ICustomer
{
    public void Initialize()
    {
        Name = "Mary";
        Town = "Tokyo";
    }

    public string Name;
    public string Town;
}