Reputation: 1206
public <return_type> getclass(string company)
{
switch (company)
{
case "cmp1": Classcmp1 temp = new Classcmp1(); return temp ;
case "cmp2": Classcmp2 temp = new Classcmp2(); return temp ;
case "cmp3": Classcmp3 temp = new Classcmp3(); return temp ;
}
}
What should be the return type of this function if classcmp1 ,classcmp2 , classcmp3 are three public classes? How to determine this dynamically?
Upvotes: 2
Views: 131
Reputation: 19294
Your getClass method must provide classes 'somehow' related : do they implement a common interface ? do they all inherit from the same parent class ?
What you should do is to have this relation explicit, then you can use this parent class or interface as a return type for your getClass method.
The choice of having an inheritance scheme vs having the classes implement the same interface is quite arbitrary. The common question is to ask yourself if your classes 'are' the same thing (inheritance) or if they 'have' the same behaviour (interface). Apples and Oranges 'are' fruits, and they 'have' a weight, so they inherit Fruits and implement IWeight... But just as well they 'are' SolidObjects and implement ITasteGood :-)
In your case i tend to believe you want to do a parent Abstract/Virtual (MustInherit in VB) Class from which your 3 classes will inherit.
I did not understand if your methods have the same signature (== parameter list). If it's not the case, remember you can send all parameters through a single object, and have again same signature.
(( Just for the record, the pattern you're implementing here (provide a class depending on a parameter) is a factory pattern. ))
Upvotes: 1
Reputation: 1373
I think the best solution in your case would be an interface like Cyril Gandon already suggested.
public interface IClasscmp {}
public class Classcmp1 : IClasscmp {}
public class Classcmp2 : IClasscmp {}
public class Classcmp3 : IClasscmp {}
public IClasscmp GetClass(string company)
{
switch (company)
{
case "cmp1": return new Classcmp1();
case "cmp2": return new Classcmp2();
case "cmp3": return new Classcmp3();
}
}
But it would also be good to use the right naming conventions for C#. It supports a better readability and the syntax highlighting here on stackoverflow will also work. ;) See: Naming Convention in c#
Upvotes: 1
Reputation: 459
Object is most generic retunn type, you may try this.
Further you do not need to initialize temp, just create.initliaze the object and return it.
public object getclass(string Company)
{
switch (Company)
{
case "cmp1": return new classcmp1();
case "cmp2": return new classcmp2();
case "cmp3": return new classcmp3();
}
}
Upvotes: 3
Reputation: 68710
Do all the classcmpX
classes share a common class/interface?
object
, the class that all types in c# extend (*).(*) well, except for pointer types.
Upvotes: 3
Reputation: 17058
You can always return an object, because all classes inherits from object:
public object getclass(string Company)
{
switch (Company)
{
case "cmp1": classcmp1 temp = new classcmp1(); return temp ;
case "cmp2": classcmp2 temp = new classcmp2(); return temp ;
case "cmp3": classcmp3 temp = new classcmp3(); return temp ;
}
}
Else you can build a class from which your three classes will inherit :
public class classcmp { }
public class classcmp1 : classcmp {}
public class classcmp2 : classcmp {}
public class classcmp3 : classcmp {}
public classcmp getclass(string Company)
{
switch (Company)
{
case "cmp1": classcmp1 temp = new classcmp1(); return temp ;
case "cmp2": classcmp2 temp = new classcmp2(); return temp ;
case "cmp3": classcmp3 temp = new classcmp3(); return temp ;
}
}
Else, you can build an interface:
public interface Iclasscmp { }
public class classcmp1 : Iclasscmp {}
public class classcmp2 : Iclasscmp {}
public class classcmp3 : Iclasscmp {}
public Iclasscmp getclass(string Company)
{
switch (Company)
{
case "cmp1": classcmp1 temp = new classcmp1(); return temp ;
case "cmp2": classcmp2 temp = new classcmp2(); return temp ;
case "cmp3": classcmp3 temp = new classcmp3(); return temp ;
}
}
The choice that you will make will influence the architecture of your program. With no more information on the use of the class you will need, it is hard to help you more than that.
Upvotes: 8