Instantiating a Generic templated class
-
Hopefully, my last battle with Generics. i have a class like so
public class ForumList<T> : IEnumerable { public void CreateMe() { **T myclass = new T();** }
Is it possible to create an instance of the class or do I need to go through Reflection to do that? Thankis, Nick-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
-
Hopefully, my last battle with Generics. i have a class like so
public class ForumList<T> : IEnumerable { public void CreateMe() { **T myclass = new T();** }
Is it possible to create an instance of the class or do I need to go through Reflection to do that? Thankis, Nick-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
You would do it like this:
public class ForumList : IEnumerable where T : class, new() { public void CreateMe() { T myclass = new T(); } }
I hope this helps.Deus caritas est
-
Hopefully, my last battle with Generics. i have a class like so
public class ForumList<T> : IEnumerable { public void CreateMe() { **T myclass = new T();** }
Is it possible to create an instance of the class or do I need to go through Reflection to do that? Thankis, Nick-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
-
You would do it like this:
public class ForumList : IEnumerable where T : class, new() { public void CreateMe() { T myclass = new T(); } }
I hope this helps.Deus caritas est
Okay so am I doing this wrong then. Because my ForumList Class should be a collection of Forum objects ( could be other objects too, not just Forum ) List<T> but I need to create new items of type "T" and I also want to extend the List class to provide some custom operations. How should the class declaration look? Is that something like public class ForumList : List<T> where T : class, new() { } -- modified at 12:34 Wednesday 16th August, 2006 -- modified at 12:36 Wednesday 16th August, 2006
-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
-
Okay so am I doing this wrong then. Because my ForumList Class should be a collection of Forum objects ( could be other objects too, not just Forum ) List<T> but I need to create new items of type "T" and I also want to extend the List class to provide some custom operations. How should the class declaration look? Is that something like public class ForumList : List<T> where T : class, new() { } -- modified at 12:34 Wednesday 16th August, 2006 -- modified at 12:36 Wednesday 16th August, 2006
-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
These should (at least be close to) be working: If you want to inherit List<>:
public class ForumList<T> : List<T> where T : class, new() {
public ForumList() {}
public T AddNewItem() {
T item = new T();
this.Add(item);
return item;
}
}If you want to keep the list private in the class:
public class FourmList<T> : IEnumerable<T> where T : class, new() {
private List<T> list;
public ForumList() {
this.list = new List<T>();
}
public T AddNewItem() {
T item = new T();
this.list.Add(item);
return item;
}
public IEnumerator<T> GetEnumerator() {
return (IEnumerator<T>)this.list.GetEnumerator();
}
}--- b { font-weight: normal; }
-
These should (at least be close to) be working: If you want to inherit List<>:
public class ForumList<T> : List<T> where T : class, new() {
public ForumList() {}
public T AddNewItem() {
T item = new T();
this.Add(item);
return item;
}
}If you want to keep the list private in the class:
public class FourmList<T> : IEnumerable<T> where T : class, new() {
private List<T> list;
public ForumList() {
this.list = new List<T>();
}
public T AddNewItem() {
T item = new T();
this.list.Add(item);
return item;
}
public IEnumerator<T> GetEnumerator() {
return (IEnumerator<T>)this.list.GetEnumerator();
}
}--- b { font-weight: normal; }
Thanks Guffa. So one last question. does the "new()" take care of all constructors(overloaded parameters) or do I list out each possible one like new(int) I.e.. new(int id) ?
-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
-
Thanks Guffa. So one last question. does the "new()" take care of all constructors(overloaded parameters) or do I list out each possible one like new(int) I.e.. new(int id) ?
-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
-
Thanks Guffa. So one last question. does the "new()" take care of all constructors(overloaded parameters) or do I list out each possible one like new(int) I.e.. new(int id) ?
-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog