x


C# Point structure

Is there a way to store xy cooridnates in a point structure instead of a Vector2 (I dont want to have to convert the floats to int every time I use them)

I tried System.Collections.Generic;, but this still does not give the Point structure for C#. Is there another collections I need to use?

Thanks!

more ▼

asked Aug 04 '12 at 03:13 PM

TobiasP gravatar image

TobiasP
89 8 15 19

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
struct Point
{
    public int x, y;
}

then use it like:

Point myPoint;

myPoint.x = 5;
myPoint.y = 3;

you can also use it with a constructor:

struct Point
{
    public int x, y;

    public Point(int px, int py)
    {
        x = px;
        y = py;
    }
}

in which case you could do this:

Point myPoint = new Point(5, 7);
more ▼

answered Aug 04 '12 at 03:50 PM

agamedesigner gravatar image

agamedesigner
223 1 4 7

Thanks! That really helped a lot!! =]!

If you read this, and have a second, I would like to know what exactly is going on. I have not really run into this type of set-up yet in my basic code skills.

Thanks again!!!

Aug 04 '12 at 04:11 PM TobiasP

Hi, a lot can be said about them so I've provided some links for you. Basically they are very similar to classes, but should generally be used for lightweight things only (unless you want to kill performance). They are a value type like int or char, as opposed to a reference type, which a class is.

More on value vs reference types:

http://msdn.microsoft.com/en-us/library/t63sy5hs(v=vs.80).aspx

http://www.c-sharpcorner.com/uploadfile/e7ad16/reference-type-and-value-type-in-C-Sharp/

Here are the links on structs, in order:

http://msdn.microsoft.com/en-us/library/ah19swz4(v=vs.71).aspx

http://www.csharp-station.com/Tutorial/CSharp/lesson12

http://www.dotnetperls.com/struct

http://stackoverflow.com/questions/7484735/c-struct-vs-class-faster

http://stackoverflow.com/questions/521298/when-to-use-struct-in-c

Hope this helps!

Aug 04 '12 at 05:01 PM agamedesigner

Thanks you!

Aug 04 '12 at 06:06 PM TobiasP
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4370
x370
x50
x47

asked: Aug 04 '12 at 03:13 PM

Seen: 1476 times

Last Updated: Aug 04 '12 at 06:06 PM