I have a custom class CurvePoint which I define a set of data items to draw on the screen using DrawCurve
I written a cast routine to convert CurvePoint to Point, but I am getting the error At least one element in the source array could not be cast down to the destination array type. when I try to use the .ToArray method in an Arraylist
I can cast the objects fine using the code:
Point f = new CurvePoint(.5F, .5F, new Rectangle(0, 0, 10, 10));
but if fails when using
Point[] Plots=(Point[])_Data.ToArray(typeof(Point));
(where _Data is an ArrayList which is populated with 5 CurvePoint objects)
Here is the full code:
public partial class Chart : UserControl
{
ArrayList _Data;
public Chart()
{
InitializeComponent();
_Data = new ArrayList();
_Data.Add(new CurvePoint(0f, 0f,this.ClientRectangle));
_Data.Add(new CurvePoint(1f, 1f, this.ClientRectangle));
_Data.Add(new CurvePoint(.25f, .75f, this.ClientRectangle));
_Data.Add(new CurvePoint(.75f, .25f, this.ClientRectangle));
_Data.Add(new CurvePoint(.5f, .6f, this.ClientRectangle));
}
private void Chart_Paint(object sender, PaintEventArgs e)
{
_Data.Sort();
e.Graphics.FillEllipse(new SolidBrush(Color.Red),this.ClientRectangle);
Point[] Plots=(Point[])_Data.ToArray(typeof(Point));
e.Graphics.DrawCurve(new Pen(new SolidBrush(Color.Black)), Plots);
}
}
public class CurvePoint : IComparable
{
public float PlotX;
public float PlotY;
public Rectangle BoundingBox;
public CurvePoint(float x, float y,Rectangle rect)
{
PlotX = x; PlotY = y;
BoundingBox = rect;
}
public int CompareTo(object obj)
{
if (obj is CurvePoint)
{
CurvePoint cp = (CurvePoint)obj;
return PlotX.CompareTo(cp.PlotX);
}
else
{ throw new ArgumentException("Object is not a CurvePoint."); }
}
public static implicit operator Point(CurvePoint x)
{
return new Point((int)(x.PlotX * x.BoundingBox.Width), (int)(x.PlotY * x.BoundingBox.Height));
}
public static implicit operator string(CurvePoint x)
{
return x.ToString();
}
public override string ToString()
{
return "X=" + PlotX.ToString("0.0%") + " Y" + PlotY.ToString("0.0%");
}
}
Can anyone sujest how to fix the code?