473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Generic ICollection and interfaces

I have a public sealed Class A that implements the interface I.
Now I want to pass an ICollection<Aas a parameter to a method that takes a
parameter of the type ICollection<Ibut I get the error:

Can't convert from System.Collections.Generic<Ato
System.Collections.Generic<I>

Shouldn't I be able to pass ICollection<Ato a method that takes
ICollection<Iwhen A implements I ?

Do I really need to cast ICollection<Ato ICollection<Ibefore passing it?

Kind Regards,
Allan Ebdrup
Oct 31 '06 #1
2 1944
Casting won't work...

What you nead is a generic method:

public void SomeMethod<T>(ICollection<Titems)
where T : ISomeInterface
{

}
You should find that you can then call this without specifying the generic
parameter:

ICollection<AmyItems = //TODO; needs A : ISomeInterface
SomeMethod(myItems);

Marc
Oct 31 '06 #2

"Marc Gravell" <ma**********@gmail.comwrote in message
news:u5**************@TK2MSFTNGP02.phx.gbl...
Casting won't work...

What you nead is a generic method:

public void SomeMethod<T>(ICollection<Titems)
where T : ISomeInterface
{

}
You should find that you can then call this without specifying the generic
parameter:

ICollection<AmyItems = //TODO; needs A : ISomeInterface
SomeMethod(myItems);
Thanks that did the trick.

Kind Regards,
Allan Ebdrup
Oct 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PMarino | last post by:
Hi all - I'm sure I'm being brain-dead here - and I think the answer is 'you can't do that', but I thought I would try. I have an interface that is Generic, with two type arguments. I want to...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
2
by: anders.forsgren | last post by:
I have a generic collection looking like this Set<T> : ICollection<T> { // Create set from existing collection. public Set(ICollection<T> objs) {/* */} // Add objects to set public void...
5
by: Brian Richards | last post by:
Without generics generally I'd dervie from CollectionBase to create typed collections. Generics mostly fill in this gap except that there are no virtual methods on List<T> so we can no longer...
2
by: Angel Mateos | last post by:
I have this structure: Class ElemBase Class Elem1 : Inherits ElemBase Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits System.ComponentModel.BindingList(Of GenElem) Class Colec1...
2
by: Edward Diener | last post by:
Following the example in the help for the generic List class, which shows the Serializable attribute being used on the generic class, like so: generic<typename T> public ref class List :...
5
by: Michi Henning | last post by:
I can pass a generic collection as ICollection<Tjust fine: static void flatCollection(ICollection<intc) {} // ... List<intl = new List<int>(); flatCollection(l); // Works fine Now I...
2
by: confused1234 | last post by:
Hi i'm trying to pass a field of type System.Collections.Generic.ICollection< FeedImage> to a function. I dont't have a clue what this icollection is. but i tried some code ...
15
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.