Question

I know that this is a pretty obvious question but from what I've seen I can't yet get it.

I don't exactly understand how Getters and Setters work in C#, for instance, I have this code for a character in a game I'm supposed to make for college:

namespace Clase_25_3_2014
{
    class Brick
    {
        private int speed { get; set; }
        private Vector2 pos { get; set; }
        private Texture2D skin { get; set; }

        public Brick(int speed, Vector2 position, Texture2D skin)
        {
            this.Speed(speed);
            this.Pos(position);
            this.Skin(skin);
        }

    }
}

Now, as it stands, where I use the class I try to call it like this:

Brick brick = new Brick(1, Vector2.Zero, mySkin);
brick.GetPos();

Now, obviously that looks weird for you guys, and that's because I haven't yet found out how am I supposed to use it correctly so that it works like a brick.getPos(); from java.

Sorry for the really obvious question, but I can't seem to find an answer for me.

Was it helpful?

Solution

You can't do GetPos because pos is private and you don't have a method called "GetPos". If you make pos public, you can just use Brick.pos to get and Brick.pos(position) to set.

Here's how you could write this class:

namespace Clase_25_3_2014
{
    class Brick
    {
        public Brick(int speed, Vector2 position, Texture2D skin)
        {
            this.Speed = speed;
            this.Pos = position;
            this.Skin = skin;
        }

        public int Speed { get; set; }
        public Vector2 Pos { get; set; }
        public Texture2D Skin { get; set; }
    }
}

Types of Class access:

// lots of explicity (is that a word? :)
public MyClass
{
  // Field
  // It is recommended to never expose these as public
  private int _myField; 

  // Property (old school, non-auto private field)
  public int MyProperty
  {
    public get
    {
      return this._myField;
    }
    public set
    {
      this._myField = value;
    }
  }

  // Property (new school, auto private field)
  // (auto field cannot be accessed in any way)
  public int MyProperty2 { public get; private set; }

  // Method (these are not needed to get/set values for fields/properties.
  public int GetMyMethod()
  {
    return this._myField;
  }
}


var myClass = new MyClass;

// this will not compile,
// access modifier says private
// Set Field value
myClass._myField = 1;  

// Get Property Value
var a = myClass.MyProperty;

// Set Property Value
myClass.MyProperty = 2;

// Get Property Value
var b = myClass.MyProperty2;

// this will not compile
// access modifier says private
// Set Property Value
myClass.MyProperty2 = 3;

// Call method that returns value
var c = myClass.GetMyMethod();

OTHER TIPS

When you declare an auto-property in C#, it will get compiled behind the scenes into get and set methods, but you do not need to even think about those. You can access the property as if it were a field.

The benefit of having the property is that you can easily swap it out for a more conventional property with a backing field so that you can provide custom logic in the getter and/or setter. Until you need that, however, it is just extra noise. The auto-property provides the syntactic sugar to avoid that noise.

Brick brick = new Brick(1, Vector2.Zero, mySkin);
Vector2 oldPos = brick.pos;
brick.pos = new Vector2.One;

Try changing to this:

    public Brick(int speed, Vector2 position, Texture2D skin)
    {
        this.Speed = speed;
        this.Pos = position;
        this.Skin = skin;
    }

And with C# you don't need this type of constructors. You can declare an object of this class without constructor with the following way:

public Brick brickTest(){
   Speed = 10,
   Position = new Vector2(),
   Skin = new Texture2D()
};
namespace Clase_25_3_2014
{
    class Brick
    {
        public int Speed { get; set; }
        public Vector2 Pos { get; set; }
        public Texture2D Skin { get; set; }

        public Brick(int speed, Vector2 position, Texture2D skin)
        {
            this.Speed = speed;
            this.Pos = position;
            this.Skin = skin;
        }

    }
}

Using outside:

Brick brick = new Brick(1, Vector2.Zero, mySkin);
Console.WriteLine(Brick.Pos);

however, Behind the scenes the compiler create for each Property:

  • Private variable storage of the Property-type.
  • Two function (Get\Set).
  • Translate the property used to their Functions.

You should just do:

Brick brick = new Brick(1, Vector2.Zero, mySkin);
Vector2 vec = brick.pos;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top