Connecting Tech Pros Worldwide Forums | Help | Site Map

How do I use Generics with interfaces?

=?Utf-8?B?TXJOb2JvZHk=?=
Guest
 
Posts: n/a
#1: Jun 23 '07
I want to have an object with a method that accepts a List of interfaces, for
example:
List<IMyInterfacemyInterfaces

Then in another object I have a List of objects which implement that
interface, like:

MyObject : IMyInterface
so the list is List<MyObject>

I cannot figure out how to pass my list of objects to that method that
accepts a list of the interfaces. It keeps saying cannot convert the values...



Tom Spink
Guest
 
Posts: n/a
#2: Jun 23 '07

re: How do I use Generics with interfaces?


MrNobody wrote:
Quote:
I want to have an object with a method that accepts a List of interfaces,
for example:
List<IMyInterfacemyInterfaces
>
Then in another object I have a List of objects which implement that
interface, like:
>
MyObject : IMyInterface
so the list is List<MyObject>
>
I cannot figure out how to pass my list of objects to that method that
accepts a list of the interfaces. It keeps saying cannot convert the
values...
Hi,

Which method are you talking about? And what exactly are you trying to do?
Add the contents of a List<MyObjectto a List<IMyInterface>?

--
Tom Spink
University of Edinburgh
Marc Gravell
Guest
 
Posts: n/a
#3: Jun 23 '07

re: How do I use Generics with interfaces?


void SomeMethod<T>(List<Tdata) where T : IMyInterface {}

(you could possibly substutute List<Tfor IList<Tor
IEnumberable<T>)

The point is that List<MyObject*does not* inherit/implement
List<IMyInterfacejust becuase MyObject : IMyInterface.

Hope this helps,

Marc

PS
Guest
 
Posts: n/a
#4: Jun 23 '07

re: How do I use Generics with interfaces?



"MrNobody" <MrNobody@discussions.microsoft.comwrote in message
news:711BCC0A-F3C1-42B0-AE94-5CB6A5B84AD6@microsoft.com...
Quote:
>I want to have an object with a method that accepts a List of interfaces,
>for
example:
List<IMyInterfacemyInterfaces
class MyClass<Twhere T : IMyInterface
{
public void MyMethod(List<TmyList){...}
}
Quote:
>
Then in another object I have a List of objects which implement that
interface, like:
This question is unclear. You are already constraining the object to a type
like

MyClass<MyListItemobjectWithList = new MyClass<MyListItem>();
objectWithList.MyMethod(new List<MyListItem>());

Do you want a second interface for the class that contains a method that
accepts a List of items of type IMyInterface?

Then you would need this

class MyClass<T: IMyClass<Twhere T : IMyInterface
{
public void MyMethod(List<TmyList){....}
}

interface IMyClass<Twhere T : IMyInterface
{
void MyMethod(List<TmyList);
}

I just did an extract interface refactoring on the first code I posted.

Quote:
MyObject : IMyInterface
so the list is List<MyObject>
>
I cannot figure out how to pass my list of objects to that method that
accepts a list of the interfaces. It keeps saying cannot convert the
values...
PS


Tom Spink
Guest
 
Posts: n/a
#5: Jun 23 '07

re: How do I use Generics with interfaces?


Tom Spink wrote:
Quote:
MrNobody wrote:
>
Quote:
>I want to have an object with a method that accepts a List of interfaces,
>for example:
>List<IMyInterfacemyInterfaces
>>
>Then in another object I have a List of objects which implement that
>interface, like:
>>
>MyObject : IMyInterface
>so the list is List<MyObject>
>>
>I cannot figure out how to pass my list of objects to that method that
>accepts a list of the interfaces. It keeps saying cannot convert the
>values...
>
Hi,
>
Which method are you talking about? And what exactly are you trying to
do? Add the contents of a List<MyObjectto a List<IMyInterface>?
>
Hi,

I think I see what you're trying to do, and as Marc has mentioned, it's a
bit of a tricky situation, as C# doesn't support generic covariance.

I see what you're trying to do now, pass a List<MyObjectto
List<IMyInterface>, but as far as C# is concerned, these two types are
distinct and not related. Like I said, C# doesn't support generic
covariance.

There are a number of ways you could approach this problem, perhaps by using
arrays instead of lists, as array covariance *is* supported. If you use
arrays, but start off with lists you can use the ToArray method of the List
object to get an array representation of the contents of the list. But
remember, you won't be able to modify those lists you're passing in - so
this approach is fine if you just want to read the array and not
concatenate/delete from it.

--
Tom Spink
University of Edinburgh
=?Utf-8?B?TXJOb2JvZHk=?=
Guest
 
Posts: n/a
#6: Jun 23 '07

re: How do I use Generics with interfaces?


Sorry I was not explaining my situation very well, but yeah Tom that's the
problem I am facing

I have this object which implements an interface and I want a method whose
signature is List<IMyInterfaceto basically accept a List<MyObjectthrough
polymorphism. But I guess tht ain't happening.

I will see if I can use arrays instead.

I am very green on this Generics topic, but if I try making a class with a
"T" somewhere in it my IDE complains. I imported System.Common.Generics, is
there some other namespace I need to use to get those T's recognized?
AlexS
Guest
 
Posts: n/a
#7: Jun 23 '07

re: How do I use Generics with interfaces?


Is that what you are talking about?

class MyObject : IMyInterface { ... }
....
List<IMyInterfacelist = new List<IMyInterface>();
list.Add(new MyObject());
....

HTH
Alex

"MrNobody" <MrNobody@discussions.microsoft.comwrote in message
news:47D17477-1EEF-4FF8-BE95-FE5A9D74586D@microsoft.com...
Quote:
Sorry I was not explaining my situation very well, but yeah Tom that's
the
problem I am facing
>
I have this object which implements an interface and I want a method whose
signature is List<IMyInterfaceto basically accept a List<MyObject>
through
polymorphism. But I guess tht ain't happening.
>
I will see if I can use arrays instead.
>
I am very green on this Generics topic, but if I try making a class with a
"T" somewhere in it my IDE complains. I imported System.Common.Generics,
is
there some other namespace I need to use to get those T's recognized?

=?Utf-8?B?TXJOb2JvZHk=?=
Guest
 
Posts: n/a
#8: Jun 23 '07

re: How do I use Generics with interfaces?




"AlexS" wrote:
Quote:
Is that what you are talking about?
>
close,

like this:

class MyObject : IMyInterface { ... }
....
public void doSomething(List<IMyInterfacelist) { ... }
.....
List<MyObjectlist = new List<MyObject>();
doSomething(list);

Tom Spink
Guest
 
Posts: n/a
#9: Jun 23 '07

re: How do I use Generics with interfaces?


MrNobody wrote:
Quote:
>
>
"AlexS" wrote:
>
Quote:
>Is that what you are talking about?
>>
>
close,
>
like this:
>
class MyObject : IMyInterface { ... }
....
public void doSomething(List<IMyInterfacelist) { ... }
....
List<MyObjectlist = new List<MyObject>();
doSomething(list);
Hi,

So we've already discussed that this isn't possible, due to the lack of
support for generic covariance. But array covariance *is* supported, have
you had any luck trying this?

--
Tom Spink
University of Edinburgh
=?Utf-8?B?TXJOb2JvZHk=?=
Guest
 
Posts: n/a
#10: Jun 23 '07

re: How do I use Generics with interfaces?


Yes, I'm sorry I forgot to mention I changed it to use Arrays and it works
fine. It is not a problem because at the point I am going to be passing it to
other functions I am not going to add or remove contents from the list so
Array is fine.
Closed Thread