I have a C#
-Class Point
with two Subclasses ColorPoint
and AmountPoint
.
Point-Class
public class Point
{
double x; // Position x
double y; // Position y
public Point(double pos_x, double pos_y) // Constructor
{
this.x = pos_x;
this.y = pos_y;
}
}
public class ColorPoint : Point
{
double color; // White value (0 to 255)
}
public class AmountPoint : Point
{
int amount; // Amount of Persons standing at this point
}
Now in my main
class I want to create a new Point in the List
This should than look something like this:
public class main
{
public main()
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AddPoint<ColorPoint>(colorList);
}
public List<T> AddPoint<T>(List<T> pointList)
where T : Point
{
pointList.Add(new T(0, 0)); // DOES NOT WORK (Cannot create instance of variable type 'T')
pointList.Add(new Point(0, 0)); // DOES NOT WORK (Cannot convert Point to T)
}
}
The variables color
or amount
can be left as null
in both cases.