0

I have no idea about what I'm doing wrong, I want to access the private integers through a get accessor but I just can't get it to work. The Map class compiles fine but I can't access the get method from one of its instances in MapViewer.

I also tried to read about it in the official documentation and the code should be fine, yet it isn't

Thanks!

public class Map {

    int xSize {get;} = 0;
    int ySize {get;} = 0;

    public Map(int xSize, int ySize){
        this.xSize = xSize;
        this.ySize = ySize;
    }

}


public class MapViewer : MonoBehaviour {

    int xSize = 20;
    int ySize = 20;

    Map map;
    Texture2D image;

    void Start () {
        map = new Map (xSize, ySize);
        image = new Texture2D(map.???, map.???); //The issue is here
    }
mustaccio
  • 18,234
  • 16
  • 48
  • 57
Bonfi
  • 121
  • 1
  • 9

4 Answers4

2

By default those properties are private, you need to declare them as public:

public int xSize {get;} = 0;
public int ySize {get;} = 0;
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • I know they are private by default, so I think it's more an issue of how I understood getters and setters. To my knowledge I would use a getter and a setter to prevent the attribute to be modified directly. In my situation I want the xSize and ySize variables to be only "getted" and not "setted", so I thought that leaving them private would only allow me to edit their values through a get. If the variables are seet to public I'm allowed to access to them even without get and set. What did I got wrong about get and set? Thanks! – Bonfi Mar 25 '16 at 22:56
1

private and public only defines the visibility of the properties. If you want them to be only set within your class you can definitely do so:

public int xSize { get; private set;}

This will let other classes "see" and evaluate the property but prohibts them from setting them. On the other hand you are still able to change the value from within the Map class.

timcbaoth
  • 669
  • 1
  • 7
  • 12
  • That's what I was looking for, thanks! With a private set, when I try to assign a value from another class, my IDE doesn't give any warnings until I compile. Weird – Bonfi Mar 25 '16 at 23:17
  • Actually most IDEs should underline an attempt to set the properties from another class, because this will result in a compile error. Intellisense in Visual Studio does at least. – timcbaoth Mar 25 '16 at 23:22
0

Your properties are private. You need to specify public access.

bottaio
  • 4,963
  • 3
  • 19
  • 43
0

When you didn't specify the access modifiers, it's hidden in IDE and default values are "private".

So actually your code is like this;

public class Map {

    private int xSize {get;} = 0;
    private int ySize {get;} = 0;

    public Map(int xSize, int ySize){
        this.xSize = xSize;
        this.ySize = ySize;
    }

}


public class MapViewer : MonoBehaviour {

    private int xSize = 20;
    private int ySize = 20;

    private Map map;
    private Texture2D image;

    void Start () {
        map = new Map (xSize, ySize);
        image = new Texture2D(map.???, map.???); //The issue is here
    }

Change it like;

public class Map {

    public int xSize {get;} = 0;
    public int ySize {get;} = 0;

    public Map(int xSize, int ySize){
        this.xSize = xSize;
        this.ySize = ySize;
    }

}

public class MapViewer : MonoBehaviour {

    private int xSize = 20;
    private int ySize = 20;

    private Map map;
    private Texture2D image;

    void Start () {
        map = new Map (xSize, ySize);
        image = new Texture2D(map.???, map.???); //The issue is here
    }

Please follow next link to learn about default access modifiers in C#:
What are the Default Access Modifiers in C#?

Access Modifiers in C#:
https://msdn.microsoft.com/en-us/library/ms173121.aspx

Note: You can use resharper to help you out. But don't use resharper like it's a modifier tool. "Learn what you should to about an error or warning" from Resharper. Resharper: https://www.jetbrains.com/resharper/

Nakro
  • 28
  • 6