tags:

views:

38

answers:

3

I wrote a bunch of code and i would like to fix it the fastest way possible. I'll describe my problem in a easier way.

I have apples and oranges, they are both Point and in the list apples, oranges. I create a PictureBox and draw the apple/oranges on screen and move them around and update the Point via Tag.

The problem now is since its a struct the tag is a copy so the original elements in the list are not update. So, how do i update them? I consider using Point? But those seem to be readonly. So the only solution i can think of is

  • Clear the list, iterate through all the controls then check the picturebox property to check if the image is a apple or orange then add it to a list

I really only thought of this solution typing this question, but my main question is, is there a better way? Is there some List<Ptr<Point>> class i can use so when i update the apples or oranges through the tag the element in the list will update as a class would?

A: 

If value semantics are not what you want, then you should be using class instead of struct. Is there any reason why you cannot convert your structs to classes?

Edit Ignore the above, I didn't quite understand what was being asked, but I think SLaks has the correct solution.

Dean Harding
He cannot change `System.Drawing.Point`.
SLaks
Oh, sorry, you're right... I think the question was edited after my answer cause I didn't quite understand what was being asked :)
Dean Harding
+1  A: 

You should make a class to contain your Points.

SLaks
I am kind of asking if theres a built in way instead of writing my own. (or if theres an alternative solution, i am trying to rewrite as little as possible, maybe i can inherit but i cant write/text code until 2hrs from now)
acidzombie24
+1  A: 

Using your naming:

public class Ptr<T> where T : struct
{
    public T Value { get; set; }

    public Ptr(T value) { Value = value; }
}

You can then make your list a List<Ptr<Point>>.

Edit

Alternatively, the absolute easiest solution would be to recreate the Point structure as a class within your own namespace.

public class Point
{
    public int X { get; set; }
    public int Y { get; set; }
}

That's obviously the barebones implementation; if you need additional functionality you'll obviously need to implement that yourself. This will, however, give you a mutable Point class.

Adam Robinson
I havent tested but wouldnt i need to rewrite a lot of code? to use .Value and not access .X, .Y directly?
acidzombie24