-1

I have an array list full of structs and everytime I try and access the object in the array list and change a value in it, it does nothing.

// Line Cache 
public ArrayList lineCache = new ArrayList();

public object drawLine(Point start, Point finish)
{
   line lineObject = new line(start, finish);
   object lineBuffer = lineCache.Add(lineObject);

   return lineBuffer;
}

public void render()
    {
        while (!renderObject.IsDisposed)
        {
            renderTarget.BeginDraw();
            renderTarget.Clear(transparencyDirect2D);

            // Line Updaters
            for (int i = 0; i < lineCache.Count; i++)
            {
                line cache = (line)lineCache[i];
                renderTarget.DrawLine(drawingBrush, cache.PointA, cache.PointB);
            }

            renderTarget.EndDraw();

            new System.Threading.ManualResetEvent(false).WaitOne(5);
        }
    }

Then in my part of the code where I make the line.

line tracer = (line)canvas.drawLine(new Point(0,0), new Point(900, 900));

        for (int i = 0; i < 200; i++)
        {
            tracer.PointB = new Point(100, 100 + i);
        }

When I set a new point it doesn't move and the point is never updated.

Require
  • 41
  • 4
  • You are getting line object from ArrayList by doing `line cache = (line)lineCache[i];`. Where in the code you are making change to `cache`? Why are you not using generic list? Did you try to debug your code? – Chetan Jul 25 '22 at 04:27
  • I show where I make the change in the bottom section. And I will try using a generic list. Edit: Generic list didn't work either – Require Jul 25 '22 at 04:33
  • You know that in C# `struct` defines [value types](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c)? – Klaus Gütter Jul 25 '22 at 04:33

1 Answers1

0

Changed the struct to class

public class line
{
    public Point PointA;
    public Point PointB;

    public line(Point start, Point finish)
    {
        this.PointA = start;
        this.PointB = finish;
    }
}
Require
  • 41
  • 4
  • 1
    You should also change your fields to properties. Note that structures should generally be immutable so, if you want your type to be mutable, it should generally be class. You don't encounter this issue if your structure can't be modified. – John Jul 25 '22 at 04:52
  • And does this solve your problem? – Klaus Gütter Jul 25 '22 at 05:05
  • @KlausGütter Well yeah thats why I posted it as an answer. – Require Jul 25 '22 at 05:18