0

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.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
krieg
  • 259
  • 1
  • 2
  • 19
  • http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing – jdphenix Mar 25 '14 at 16:26
  • simply `var pos = brick.pos;` or `brick.pos = something;` - BTW, if you're working with C# you should honor C#'s naming convention. Use `ProperCase` for your properties and give them a meaningful name (`"pos"` does not mean anything, it should be `Position` or something). BTW, java doesn't have any of these features because [java sucks](http://whyjavasucks.com/) while [C# rocks](http://www.slideshare.net/jeffz/why-java-sucks-and-c-rocks-final) – Federico Berasategui Mar 25 '14 at 16:30
  • Yeah C# feels cooler than Java, so you use ProperCase even for variables in C#? So variables start with Capitals aswell? – krieg Mar 25 '14 at 16:32
  • @krieg [Properties](http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx) are different from "variables" (I guess you're referring to fields?) – Federico Berasategui Mar 25 '14 at 16:35
  • Your code sample does not compile... You don't use upper case for *local variables* and *fields* in C#, but your sample shows *properties* which are usually upper case (unclear why you want private property, but it is your call). – Alexei Levenkov Mar 25 '14 at 16:37
  • Well, I've just started to realize that private does not work like it did in Java. – krieg Mar 25 '14 at 16:39
  • @krieg - I believe `private` means almost the same thing in C# and Java - access modifier; what you seem to be confused with is fields vs. properties vs. [auto-implemented properties](http://msdn.microsoft.com/en-us/library/bb384054.aspx) (which you used). Consider reading [C# for Java devs on MSDN](http://msdn.microsoft.com/en-us/library/ms228386%28v=vs.90%29.aspx) like [Properties](http://msdn.microsoft.com/en-us/library/ms228386%28v=vs.90%29.aspx) – Alexei Levenkov Mar 25 '14 at 16:44

5 Answers5

4

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();
Erik Philips
  • 48,663
  • 7
  • 112
  • 142
John Darvill
  • 1,225
  • 9
  • 17
  • Yeah, but where does the Get and Set come in then? I made it private to force the Get and Set, or do they work differently in C#? – krieg Mar 25 '14 at 16:29
  • 1
    I don't understand what *work differently* means. You haven't give any context then different from something else. Also don't confuse [Access Modifiers](http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx) with the [difference between Fields and Properties](http://stackoverflow.com/questions/653536/difference-between-property-and-field-in-c-sharp-3-0) – Erik Philips Mar 25 '14 at 16:32
  • 4
    @Krieg, to add to JD41's post, You could consider whether or not code outside of the class can set your properties. If not, consider using the `{get; private set;}` access modifiers for your properties. Your constructor will be able to set values, but code outside of the class will only be able to read them (if this is what you need of course). – Jon Bellamy Mar 25 '14 at 16:34
  • 1
    @krieg bottom line: forget java. it is a hopeless dinosaur from the 90's, while C# is a modern, strongly typed, beautiful, expressive programming language. – Federico Berasategui Mar 25 '14 at 16:50
  • I can see that, hopefully I can catch up to it. – krieg Mar 25 '14 at 17:09
2

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()
};
lpaloub
  • 858
  • 2
  • 8
  • 21
2

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;
Nelson
  • 31
  • 3
1
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.
dovid
  • 5,945
  • 3
  • 31
  • 66
0

You should just do:

Brick brick = new Brick(1, Vector2.Zero, mySkin);
Vector2 vec = brick.pos;
omriman12
  • 1,439
  • 3
  • 23
  • 42