I'm messing around trying to learn C# in Visual Studio. I have only basic coding knowledge, and I bought C# 5.0 in a nutshell. I'm loving the book, and trying to make mini programs out of everything I read to help make it stick. I thought structs were something simple, but for whatever reason I just can't get a struct to work.
So here's a brief and ultra basic example from the book.
public struct Point {
int x, y;
public Point(int x, int y) { this.x = x; this.y = y; }
}
Point p1 = new Point();
Point p2 = new Point(1, 1);
It works fine. But now say I want to manipulate the x and y variables in p1 or p2. I've tried so much, and I can't get it to work.
public struct Point {
public int x;
}
Point p1 = new Point();
p1.x = 10;
This won't work. When I try to set p1.x to 10, I get an error. It says p1 is a "field" but is used like a "type."
There's probably something simple I'm missing, but my patience for trial and error has run out. So what am I doing wrong? I understand the basic concept of why a struct is useful, but I need to be able to actually use it once I make it!