0

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!

pbaris
  • 4,525
  • 5
  • 37
  • 61
user1558737
  • 239
  • 1
  • 2
  • 3
  • 6
    You need to put the code *inside* a function/method. The type and member variable *declarations* can be in an enclosing type, but the statement `p1.x = 10` *must* be in a function/method. Alternatively, you can initialize it as: `Point p1 = new Point() { x = 10; };` (this is still part of the *declaration* bit). –  Sep 12 '12 at 00:44
  • 1
    As a side note, see http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil – Tim S. Sep 12 '12 at 02:23
  • Struct property setters are problematic. The proper solution, however, is not to limit struct mutation to replacement, but rather to simply expose public fields, especially if the purpose of a struct is simply to hold a number of related variables, rather than encapsulate functionality. Note that there isn't really any such thing as a non-trivial "immutable" structure type. Immutability is a property of structure *instances*. – supercat Sep 13 '12 at 19:34
  • So-called "immutable" structures don't allow mutation except by doing a memberwise copy of another struct instance. A statement `myPoint = new Point(5);` doesn't replace `myPoint` with a new instance. It generates a new temporary instance, initializes it, and then overwrites all fields (public and private) in `myPoint` with the corresponding values in the temporary point, which is then discarded. If `myPoint` is writable, all of its fields may be mutated via such assignment. If it isn't writable, none of its fields may be mutated via any means whatsoever. – supercat Sep 13 '12 at 19:41

2 Answers2

0
class Program
{
    public struct Point
    {
        int x, y;
        public Point(int x, int y) { this.x = x; this.y = y; }
    }

    static void Main(string[] args)
    {
        Point p1 = new Point();
        Point p2 = new Point(1, 1);
    }
}

Just like @pst mentioned. p1 and p2 need to be inside a method. In this case they are inside the main method.

pbaris
  • 4,525
  • 5
  • 37
  • 61
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
0

Tono Nam's answer is correct but incomplete.

To set the values of x and y the way you want to, you will also need to set the correct access modifier for x and y e.g.

class Program
{
    public struct Point
    {
        public int x, y;
        public Point(int x, int y) { this.x = x; this.y = y; }
    }

    static void Main(string[] args)
    {
        var p1 = new Point();
        var p2 = new Point(1, 1);
        p1.x = 1;
        p1.y = 1;
    }
}

You'll want to use public to access x and y from any class.

You'll want to use internal if you only want to access x and y from within the same class where the struct Point is defined.

Tod Thomson
  • 4,773
  • 2
  • 33
  • 33