471,594 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

MyJaggedIntArr(ICollection<ICollection<int>> items) rejects List<List<Int>>

Hi,

*background*
I want a class containing an int[][] (a list of sets of integer). This
should be hidden for the user and he/she should be able to insert
his/her favourite data structure so to be a little fancy I thought I'd
do a Ctor like this:

// this is the "core": private int[][] sets;

public MyJaggedIntArr(ICollection<ICollection<int>items)
{
this.sets = new int[items.Count][];
int c = 0;
foreach (ICollection<intset in items)
{
sets[c] = new int[set.Count];
set.CopyTo(sets[c++], 0);
}
}

*the problem*
This works fine if I insert an int[][] into the Ctor, but I was unable
to pass a List<List<Int>I got the following exception:

Argument '1': cannot convert from
'System.Collections.Generic.List<System.Collection s.Generic.List<int>>'
to
'System.Collections.Generic.ICollection<System.Col lections.Generic.ICollection<int>>'

*the question*
Is there some even more basic interface than ICollection that includes
lists and ICollection that I could use? Or should I just make four
constructors?

1 - List<List<Int>>
2 - List<ICollection<Int>>
3 - ICollection<List<Int>>
4 - ICollection<ICollection<Int>>

I also might need to add a fifth one
(5 - int[,])

Or should I just force the user to insert int[][]?

thanks

/Per

Dec 19 '06 #1
2 2115
Hello,

If the users will always be adding int[][] then I would force that. Do
they really need the ability to add generic lists of ints?

--
Cheers,
Gary
http://www.garyshort.org/
per9000 wrote:
Hi,

*background*
I want a class containing an int[][] (a list of sets of integer). This
should be hidden for the user and he/she should be able to insert
his/her favourite data structure so to be a little fancy I thought I'd
do a Ctor like this:

// this is the "core": private int[][] sets;

public MyJaggedIntArr(ICollection<ICollection<int>items)
{
this.sets = new int[items.Count][];
int c = 0;
foreach (ICollection<intset in items)
{
sets[c] = new int[set.Count];
set.CopyTo(sets[c++], 0);
}
}

*the problem*
This works fine if I insert an int[][] into the Ctor, but I was unable
to pass a List<List<Int>I got the following exception:

Argument '1': cannot convert from
'System.Collections.Generic.List<System.Collection s.Generic.List<int>>'
to
'System.Collections.Generic.ICollection<System.Col lections.Generic.ICollection<int>>'

*the question*
Is there some even more basic interface than ICollection that includes
lists and ICollection that I could use? Or should I just make four
constructors?

1 - List<List<Int>>
2 - List<ICollection<Int>>
3 - ICollection<List<Int>>
4 - ICollection<ICollection<Int>>

I also might need to add a fifth one
(5 - int[,])

Or should I just force the user to insert int[][]?

thanks

/Per
Dec 19 '06 #2
If the users will always be adding int[][] then I would force that. Do
they really need the ability to add generic lists of ints?
Hi Gary,
since I don't know how users will store their int's I'd be happy to
find something generic. Also int[][] is an implementation of
ICollection<ICollection<Int>so the Ctor above can deal with it.

But on the other hand: do they really need a very generic constructor?
No, probably not.

Also I would like the user to not know/care about how I store the int's
since they might get funny ideas (I know I get them all the time).

There is really little trouble for me to write N^2 Constructors. But it
really intrigued me that List<Twas *not* an implementation of
ICollection<Tso it is also a philosophical/structural question.

/Per

Dec 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Donnal Walter | last post: by
4 posts views Thread by mb | last post: by
9 posts views Thread by Alpha | last post: by
2 posts views Thread by hsuntn | last post: by
13 posts views Thread by PetterL | last post: by
reply views Thread by leo001 | last post: by

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.