0

Currently I store X and Y using

Point[] points1 = new Point[] { 
    new Point { X = 262, Y = 321 }, 
    new Point { X = 325, Y = 334 }, 
    new Point { X = 278, Y = 387 }
};

how can I set variable 'points1' globally ? so I can using loop to pull the data X and Y.

Sample Code what I've tried

Point[] points1 = new Point[]{};

for(int i = 0 ; i <10 ;i ++)
{
  points1 = new Point[] { new Point { X = i, Y = i++}};
}

but always get the last result instead of in array list.

3
  • you can set the default values at constructor in point class. Commented Sep 30, 2018 at 15:16
  • Why do you increment i twice, once in the declaration of your loop and once at Y = i++? Commented Sep 30, 2018 at 15:19
  • The for-loop body is wrong, use points1[i] = new Point {...}; Commented Sep 30, 2018 at 15:23

4 Answers 4

3

Instead of creating a new array (which is of type Point[]) at each iteration, you should create a new array-element (which is of type Point) and put it into the array:

for(int i = 0; i < 10; i++)
{
    points1[i] = new Point { X = i, Y = i++ };
}

Furthermore you are incrementing i twice, once in the for-loop-declaration and once when creating the new Point. Therefor you get the following values for your X- and Y-coords:

{ 0, 2, 4, 6, 8 }. 

To create 10 points instead of only 5 you should omit one of those incrementations.

Anyway I would suggest to use a List<Point> instead, which is more dynamic as you can easily add and remove elements to it:

List<Point> points = new List<Point>();
points.Add(new Point(...));
Sign up to request clarification or add additional context in comments.

4 Comments

sorry, was typo.. will change the x and y value later..i did try the solution and i get this error '{"Index was outside the bounds of the array."}' hmm
How did you declare the array? Of course you need something like points = new Point[10] before you can add data to the array.
Do you want five elements in your array or ten?
Icic!! sorry my mistake.. may i know how can i do it dynamically? without assign a fix value?
1
for(int i = 0 ; i <10 ;i ++)
{
points1 = new Point[] { new Point { X = i, Y = i++}};
}

doesn't set the value of each item in the array. It reassigns points1 to be a new array each iteration. Notice that points1 is being assigned, not an element of points1. To do this, you need to assign new Point values to points1[i] instead.

Comments

1

You can set the points for that array directly:

Point[] points1 = new Point[10];
for (int i = 0; i < 10; i++)
{
    points1[i] =  new Point { X = i, Y = i };
}

If you dont have a fixed number of point you have to use List instead of array. Here is an example:

List<Point> points1 = new List<Point>();
for (int i = 0; i < 10; i++) // can be any limit
{
    points1.Add(new Point { X = i, Y = i++ });
}
// you can convert to array at any time
var points = points1.ToArray();
Console.WriteLine(points1);

1 Comment

I update my answer, and now includes the lists solution as well
0
        List<Point> points1 = new List<Point>();

        for (int i = 0; i < 10; i++)
        {
            points1.Add(new Point { X = i, Y = i++ });
        }

1 Comment

Do add some text/context. Code-only answers are less helpful and frowned upon! Also: I don't think he want to add a point. But using List is generally recommended..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.