Generics problem
-
There are following classes:
public class X
{
Int32[] a;
public List<Int32> xxx()
{ ... }
}public class Y
{
List<Int32> a;
public List<Int32> xxx()
{ ... }
}What I am trying to do is to find a generic way to make the class of similar descent:
public class GenericClass<T> where T : IEnumerable
{
T a;
public List</*Type of the elements in the IEnumerable T*/> xxx()
{ .... }
}No ideas come to my head. Maybe some will come to yours'? I understand that the problem is that IEnumerable is not generic but maybe there will be any clever solutions :)
-
There are following classes:
public class X
{
Int32[] a;
public List<Int32> xxx()
{ ... }
}public class Y
{
List<Int32> a;
public List<Int32> xxx()
{ ... }
}What I am trying to do is to find a generic way to make the class of similar descent:
public class GenericClass<T> where T : IEnumerable
{
T a;
public List</*Type of the elements in the IEnumerable T*/> xxx()
{ .... }
}No ideas come to my head. Maybe some will come to yours'? I understand that the problem is that IEnumerable is not generic but maybe there will be any clever solutions :)
public class Foo<T> : IEnumerable<T> {
private List<T> someList;
IEnumerator<T> IEnumerable<T>.GetEnumerator();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator();
}Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
There are following classes:
public class X
{
Int32[] a;
public List<Int32> xxx()
{ ... }
}public class Y
{
List<Int32> a;
public List<Int32> xxx()
{ ... }
}What I am trying to do is to find a generic way to make the class of similar descent:
public class GenericClass<T> where T : IEnumerable
{
T a;
public List</*Type of the elements in the IEnumerable T*/> xxx()
{ .... }
}No ideas come to my head. Maybe some will come to yours'? I understand that the problem is that IEnumerable is not generic but maybe there will be any clever solutions :)
I don't know if I understand your problem, but if I'm correct, you should do
public class GenericClass<T> { T a; public List<T> xxx() { .... } }
If it's not what you want, write about your problem in more details.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
-
I don't know if I understand your problem, but if I'm correct, you should do
public class GenericClass<T> { T a; public List<T> xxx() { .... } }
If it's not what you want, write about your problem in more details.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
You see, if I instantiate this class like this:
new GenericClass<Int32[]>();
, then what xxx() returns is List<Int32[]>, although it should return List<Int32>. Now, if I instantiate the class this way:
new GenericClass<List<Int32>>();
, then xxx() will return List<List<Int32>>, but it should also return List<Int32>. That is why I think that IEnumerable is the way to go for T. I am starting to think that my idea is conceptually wrong, since I cannot think of anything that would work.
-
There are following classes:
public class X
{
Int32[] a;
public List<Int32> xxx()
{ ... }
}public class Y
{
List<Int32> a;
public List<Int32> xxx()
{ ... }
}What I am trying to do is to find a generic way to make the class of similar descent:
public class GenericClass<T> where T : IEnumerable
{
T a;
public List</*Type of the elements in the IEnumerable T*/> xxx()
{ .... }
}No ideas come to my head. Maybe some will come to yours'? I understand that the problem is that IEnumerable is not generic but maybe there will be any clever solutions :)
Your problem is you need
T
to be the type of element, not a collection of so yourIEnumerable
idea isn't going to work. I think you need something like this:public class GenericClass<T>
{
private List<T> a = null;public GenericClass(IEnumerable<T> items) { if (items != null) a = new List<T>(items); } public List<T> Xxx() { return a; }
}
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Your problem is you need
T
to be the type of element, not a collection of so yourIEnumerable
idea isn't going to work. I think you need something like this:public class GenericClass<T>
{
private List<T> a = null;public GenericClass(IEnumerable<T> items) { if (items != null) a = new List<T>(items); } public List<T> Xxx() { return a; }
}
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
You're welcome :)
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)