I have 2 classes:
public class Articles
{
private string name;
public Articles(string name)
{
this.name = name;
}
public void Output()
{
Console.WriteLine("The class is: " + this.GetType());
Console.WriteLine("The name is: " + name);
}
}
And
public class Questionnaire
{
private string name;
public Questionnaire(string name)
{
this.name = name;
}
public void Output()
{
Console.WriteLine("The class is: " + this.GetType());
Console.WriteLine("The name is: " + name);
}
}
I want to write a method, that takes an integer (1 meaning Articles
should be returned, 2 meaning Questionnaire
) and a name.
This method must return an instance of one of those two classes:
public [What type??] Choose(int x, string name)
{
if (x == 1)
{
Articles art = new Articles(name);
return art;
}
if (x == 2)
{
Questionnaire ques = new Questionnaire(name);
return ques;
}
}
What return type should I use, so I can call Output()
on the result?
dynamic
), but consider using strongly typed solutions shown in answers. – Hardandfast