How to use generic abstract class as return type ? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to use generic abstract class as return type ?

kkris1983kkris1983 Posts: 2Member
Hi there!

I have something like this:

[code]
abstract class BaseClass
{
protected BaseClass(){}
}

class Class1 : BaseClass
{
public static Class1 Instance = new Class1();
private Class1(){}
}

class Class2 : BaseClass
{
public static Class2 Instance = new Class2();
private Class2(){}
}

...
public BaseClass Method(int z) where T: BaseClass
{
switch(z)
{
case 1:
return Class1.Instance;
case 2:
return Class2.Instance;
}
}
[/code]

That is very important that those classes cannot be instantiated since their construstors are private so we cannot do like

[code]public BaseClass Method(int z) where T: BaseClass, **new()**[/code]

The problem arises that I cannot in switch clause return those Class1.Instance because it says "cannot convert Class1 to return type BaseClass" How can I use abstract class as return type ?? I just can not work this out. Would appreciate for any assisstance here.

Sign In or Register to comment.