hi,
I have to do application like Paint. I have to paint Line, Rectangule and Ellipse. I want to use STATE deisgn patern. I made class Line, Rect and Ellipse and Interface IPaint.
In class main I made IPaint paint, and List<IPaint> list= new Line<IPaint>(); Then by 3 buttons i add paint = new Line(); or rect or ellipse() depends on wich button are clicked. But when I`m use list.Add(paint) this not working correct.
When i add first element it`s OK, But when i add second element then first element look like second. When i`m add thirth element then first, and second el. have value like thitrh.
How can i do this right ?
/////edit
Yes it`s just a typo in post. In code is
List<IPaint> list = new List<IPaint>;
Im in work now and i dont have acces to code.
But full code look like:
Interface IPaint
Interface IPaint
{
void Paint{PrintEvenArgs e}
Point BeginPoint{get;set;}
Point EndPoint(get;set)
}
Class Line, Rect and Ellipse look the same:
Class Line: IPaint{
public Begin/Endpoint {get;set;}
void Paint(PrintEventArgs e)
{
Pen p = new Pen(Color red,2)
e.drawLine(p,BeginPoint,EndPoint)
}
}
In Form1 i do:
Class Main{
IPaint point;
List<IPaint> lista = new List<IPaint>();
Void Main
{
Inicianization();
}
event MouseDown(Obiect Sender, MouseArgs e)
{
point.BeginPoint = e.location;
}
event mouseMove(...)
{
point.EndPoint = e.location;
Invalidate();
}
event mouseup(..)
{
lista.Add(point);
}
event Form1_Paint(Object Sender, PrintEventArgs e)
If (!Point.BeginPoint.IsNull & !Point.EndPoint.IsnuLL)
{
point.Paint(e);
}
for (int i =0, i< lista.Count, i++)
{
lista[i].Paint(e);
}
event button1_click(...)
{
point = new Line();
}
}
Problem is in add to list. When i Add first element with point`s (2,2)m second (3,3) then first change value to second element on the list, when i add thirth with points (5,6) then first and secont element change value to thirth element and list look like:
list[0] - Begin point - 5
- EndPoint - 6
list[1] - Begin point - 5
- EndPoint - 6
list[2] - Begin point - 5
- EndPoint - 6
It is look like when i add to list object point, it is add by reference. But why ?