Using single method to return possible different instance types

200 views Asked by At

So I have got these 2 instance types "FirstType" and "SecondType" which inherit from the mother class "ContaBancaria". They both return text from different textboxes. Basically, they do the same thing, but I need 2 instances for 2 different list types (I probably don't think the list has anything to do with my question, so I'll proceed not to go in detail)

Here are the instances:

    private FirstType AddTypeFirst()
    {
        return new FirstType(textBoxNumber.Text,
            textBoxBalance.Text,
            textBoxName.Text,
            textBoxAddress.Text,
            textBoxBirth.Text);
    }

    private SecondType AddTypeSecond()
    {
       return new SecondType(textBoxNumber.Text,
            textBoxBalance.Text,
            textBoxName.Text,
            textBoxAddress.Text,
            textBoxBirth.Text);
    }

Is there a way to return these 2 instances with the same method type?

EDIT: What I meant was to return these 2 different types of instances with 1 single method, for example:

private [type?] AddInstance()
{
   return new [type*] textBoxNumber.Text,  //* the type could be FirstType or SecondType
            textBoxBalance.Text,
            textBoxName.Text,
            textBoxAddress.Text,
            textBoxBirth.Text);
}

EDIT 2:

ContaBancaria looks like this:

abstract class ContaBancaria
{
    public string number { get; set; }
    public string balance { get; set; }

    public Client data { get; set; }
}

And, since there's Client...

class Client
{
    public string name;
    public string address;
    public string birth;
}

Hope you get me.

2

There are 2 answers

0
Dmitry On BEST ANSWER

You can use generic method and derrived classes I think. For example, you have two classes and you want to receive one of them. Those classes are named "FirstSon" and "SecondSon" and both of them are derrived from class "Father".

class Father
{
    string myName;
    public string MyName
    {
        get { return myName; }
        set { myName = value; }
    }
    public Father()
    {
        myName = "John";
    }
}

class FirstSon : Father
{
    string mySecondName;
    public string MySecondName
    {
        get { return mySecondName; }
        set { mySecondName = value; }
    }

    public FirstSon()
    {
        mySecondName = "Bill";
    }
}

class SecondSon : Father
{
    int age;
    public int Age
    {
        get { return age; }
        set { age = value; }
    }
    string mySecondName;
    public string MySecondName
    {
        get { return mySecondName; }
        set { mySecondName = value; }
    }
    public SecondSon()
    {
        mySecondName = "Drake";
        age = 21;
    }
}

And you have method GetObject(). This method is generic. It receives type of class, then checks what type it has received and returnes the new object with the same type.

    public static T GetObject<T>() where T: Father
    {
        var firstSon = new FirstSon();
        var secondSon = new SecondSon();
        if (firstSon.GetType() == typeof(T))
            return (T)Convert.ChangeType(firstSon, typeof(T));
        return (T)Convert.ChangeType(secondSon, typeof(T));
    }

It uses method Convert.ChangeType(object value, Type conversonType) and allows you to convert your object to your type.

But I am not convinced that this is a good idea according to How do I make the return type of a method generic?

1
NetMage On

Assuming you want to return the proper type based on the list being added to, you will need to write your own generic Add function, and use Reflection to figure out the type:

public static class Ext {
    public static void AddInstancia<T>(this List<T> aList) where T : class {
        if (typeof(T) == typeof(FirstType))
            aList.Add(AddTypeFirst() as T);
        else
            aList.Add(AddTypeSecond() as T);
    }
}

I see no good reason to do this - after all, you know the type of the list, just call the correct function for that list...

Instead of using Reflection, you could also use dynamic if you add some functions to each sub-type:

public class FirstType : Parent {
    public FirstType MakeChild() {
        return new FirstType();
    }
}

public class SecondType : Parent {
    public SecondType MakeChild() {
        return new SecondType();
    }
}

public static class Static<T> where T : new() {
    public static dynamic Value = new T();
}

public static class Ext {
    public static void AddInstance<T>(this List<T> aList) where T : new() {
        aList.Add(Static<T>.Value.MakeChild());
    }
}

Which you can call like

var list1 = new List<FirstType>();

list1.AddInstance();