1

I want to explain my title of this question.

I have the base class

 public class BaseClass { /* stuff */ }

And other two classes which inherits BaseClass

  public class Test1: BaseClass {  ... }

  public class Test2: BaseClass { ... }

Ok then let assume with generic class (in my project it is very complex)

  public GenericClass<T> : IBase<T> where T: BaseClass, Test1, Test2

Sometimes I need to use only BaseClass otherwise Test1 or Test2.

I have a function:

  public int Create ( T obj){
    if( obj is Test1) { return aManager.Create((Test1)obj); } // the cast is OK
    else if(obj is Test2) { return bManager.Create((Test2)obj);}  // error cast
  }

I have also aManager.Create (Test1 obj) and bManager.Create(Test2 obj)

Why in the else row I have error in cast?

Where is my mistake?

PS: If I change the order between Test1 and Test2 then in first if will occur casting error and in else will be ok.

7
  • 8
    where T: BaseClass, Test1, Test2 combines classes with AND, not OR, so your class has to satisfy all these constraints. Commented Mar 29, 2013 at 7:40
  • stackoverflow.com/questions/1176908/… Commented Mar 29, 2013 at 7:41
  • No matter, is a simple class which add an object Commented Mar 29, 2013 at 7:46
  • @SnakeEyes: that matters, the way you use if else in order to do just casting is really messy, why you process inside Create method Commented Mar 29, 2013 at 7:50
  • I think the problem is here public int Create ( T obj){. You need to declare it public int Create (object obj){ Commented Mar 29, 2013 at 7:50

2 Answers 2

0

I think you have misunderstood how generic type constraints work. Let me start by first answering your question.

You actually only need the signature like this.

public GenericClass<T> : IBase<T> where T: BaseClass

which means, the type T is any type that derives from type BaseClass or the BaseClass itself.

It will very well accept any instances of type BaseClass, Test1 and Test2.

Further, I'd like to suggest an improvement in your code..

public int Create ( T obj){
    if( obj is Test1) { return aManager.Create(obj as Test1); } // the cast is OK
    else if(obj is Test2) { return bManager.Create(obj as Test2);}  // error cast
  }

However, I believe that the design of your code has some loop holes. If you could explain your context in more detail, you might get better design solutions for your problem. Since I believe this is more to do with the design than the generics as it seems you're using the wrong tool for the job.

Sign up to request clarification or add additional context in comments.

Comments

0

As far as both the Test1 and the Test2 are class, this line is not true.

public GenericClass<T> : IBase<T> where T: BaseClass, Test1, Test2

Because just one base class is allowed in generic type constrains and it must come as the first constraint, however you can have multiple interface types as constraint. See this

To see what I mean change the Create method like this and you will see the error:

public int Create ( T obj)
{
//if( obj is Test1) { return aManager.Create((Test1)obj); } // the cast is OK
//else if(obj is Test2) { return bManager.Create((Test2)obj);}  // error cast
    return 1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.