Constructing a Generic Class from several similar classes
-
I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-
public class DecimalClass
{
public int x;
public string y;
public List<decimal> Details;
}public class StringClass { public int x; public string y; public List<string> Details; }
Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-
public class MyClass<T>
{
public int x;
public string y;
public List<T> Details;
}and having a
List<MyClass<T>> myClasses
in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-
public class DecimalClass
{
public int x;
public string y;
public List<decimal> Details;
}public class StringClass { public int x; public string y; public List<string> Details; }
Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-
public class MyClass<T>
{
public int x;
public string y;
public List<T> Details;
}and having a
List<MyClass<T>> myClasses
in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
Hmmmm. Ultimately all classes derive from
object
, so you could store the data in aList<object>,
but this wouldn't give you any advantage over storing as an ArrayList. This means you'd have a lot of boxing/unboxing to take care of, and you'd end up having to use reflection to work out what type to convert it back to. Alternatively, you could store it in aDictionary<Type, List<object>>
and use that to avoid having to use the reflection part. Just a thought.Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hmmmm. Ultimately all classes derive from
object
, so you could store the data in aList<object>,
but this wouldn't give you any advantage over storing as an ArrayList. This means you'd have a lot of boxing/unboxing to take care of, and you'd end up having to use reflection to work out what type to convert it back to. Alternatively, you could store it in aDictionary<Type, List<object>>
and use that to avoid having to use the reflection part. Just a thought.Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Hi Pete, Thanks for your input. I think I will have to go with
List<object>
, but decided to pass the Type in as a parameter to the constructor for the sub class with a private Type variable, as it meant the least changes to existing code, though if I had to start from scratch again, I would have used the dictionary as you suggested. Thanks again for your time.When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-
public class DecimalClass
{
public int x;
public string y;
public List<decimal> Details;
}public class StringClass { public int x; public string y; public List<string> Details; }
Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-
public class MyClass<T>
{
public int x;
public string y;
public List<T> Details;
}and having a
List<MyClass<T>> myClasses
in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
Make the base class
public class MyBaseClass
{
public int x;
public string y;
public IEnumerable Details;
}And derive the generic class from it.
public class MyClass : MyBaseClass
{
public MyClass()
{
Details = new List();
}
}That way, you could hold a
List baseClassList
in your Main class.Ciao, luker
-
Make the base class
public class MyBaseClass
{
public int x;
public string y;
public IEnumerable Details;
}And derive the generic class from it.
public class MyClass : MyBaseClass
{
public MyClass()
{
Details = new List();
}
}That way, you could hold a
List baseClassList
in your Main class.Ciao, luker
Unfortunately, holding a List of MyBaseClass I would need to use reflection to cast from MyBaseClass to MyClass in the Main Class and so I would be back to where I started, as I need to know the Type of object I am dealing with. Thanks for your thoughts.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-
public class DecimalClass
{
public int x;
public string y;
public List<decimal> Details;
}public class StringClass { public int x; public string y; public List<string> Details; }
Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-
public class MyClass<T>
{
public int x;
public string y;
public List<T> Details;
}and having a
List<MyClass<T>> myClasses
in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
I think you need to explain more thoroughly what you are actually trying to do. The list of 'lots of stuff' seems confused and that should almost certainly be a List<BaseClass> (or an interface, possibly) where BaseClass defines all the things that you want to do on the different MyClass types. Similar to the answer above, I would factor out the common, non generic parts into a base class (note that this isn't necessarily the same BaseClass referenced in the first paragraph, though it could be; the question is not detailed enough to be able to tell):
public abstract class BaseClass {
public int x;
public string y;
public virtual IEnumerable BaseDetails { get; set; }
}public class MyClass<T> : BaseClass {
public IList<T> Details { get; set; }
public override IEnumerable BaseDetails {
get { return Details; }
// The setter will fail if it's the wrong type, probably
// what you want
set { Details = (IList<T>)value; }
}
}BaseClass should also define virtual methods for whatever operations you are going to want to do on things pulled out of the list.
-
I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-
public class DecimalClass
{
public int x;
public string y;
public List<decimal> Details;
}public class StringClass { public int x; public string y; public List<string> Details; }
Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-
public class MyClass<T>
{
public int x;
public string y;
public List<T> Details;
}and having a
List<MyClass<T>> myClasses
in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
Maybe this helps:
public class Test {
public void Run() {
List<MyBase> all=new List<MyBase>();
all.Add(new MyGeneric<int>());
all.Add(new MyGeneric<string>());
foreach(MyBase b in all) {
log("Holding a list of "+b.MyType);
}
}public class MyBase { public Type MyType; public MyBase(Type type) { MyType=type; } } public class MyGeneric<T> : MyBase { public int x; public string y; public List<T> Details; public MyGeneric() : base(typeof(T)) {} }
}
:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Unfortunately, holding a List of MyBaseClass I would need to use reflection to cast from MyBaseClass to MyClass in the Main Class and so I would be back to where I started, as I need to know the Type of object I am dealing with. Thanks for your thoughts.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman