Gad
Gad

Reputation: 42306

casting BindList<Classname> to BindingList<Interface>

I have an object (class A) that implements an interface I.

My object C has a BindingList listA

At one point I need to perform the following cast:

BindingList<I> funcName(){
   ...
   return (BindingList<I>) C.listA;
}

But this does not compile because of a cast error.

How should I go and do that?

Upvotes: 2

Views: 890

Answers (1)

John Bledsoe
John Bledsoe

Reputation: 17662

This is a covariance issue. It's been addressed in .NET 4.0 but not for all enumerable types, and I don't think it's been addressed for BindingList<T>.

I think your only option is to create a new instance of BindingList as follows:

BindingList<I> funcName(){
   ...
   return new BindingList<I>(C.listA);
}

Alternately, you could declare your C.listA field as a BindingList<I> and just add instances of your class to it.

Upvotes: 3

Related Questions