30

I understand how to create a getters and setters

public myClass
{
    public int myVal { get; set; }

    // more stuff
}

but I don't understand how to call it later on.

public myOtherClass
{
    public myOtherClass()
    {
         myClass localMyClass = new myClass();

         localMyClass.???set??? = 42;  
         // Intelisense doesn't seem to give any obvious options after I enter 
         // the period.
    }
}

How should I set the value of myVal in localMyClass?

BIBD
  • 15,107
  • 25
  • 85
  • 137
  • If it's not showing up in intellisense then, perhaps it's not valid compilable code? There is no standard `ing` type and it's pretty hard to miss finding a property setter, if it is indeed listed, even without a "set" in front of it. Also, the chosen naming convention *hurts my eyes*. –  Mar 13 '13 at 04:16

5 Answers5

25
localMyClass.myVal = 42;

Getters and setters let you treat the values like public properties. The difference is, you can do whatever you want inside the functions that do the getting and setting.

Examples:

store other variables

private int _myVal, myOtherVal;
public int MyVal { get; set { _myVal = value; myOtherVal++; } }

make numbers up / return constants

public int MyVal { get { return 99; } set; }

throw away the setter

private int _myVal;
public int MyVal { get { return _myVal; } set { ; } }

In each of these cases, the user will feel like it's just a public data member, and simply type

localMyClass.myVal = 42;
int i = localMyClass.myVal;

The gettors and settors let you make an implementation of your own. Also, as Hogan says, "There are a number of libraries and add-ons [e.g. MVC.NET] that require you to use getter and setter functions" - even if it's for the trivial {get; set;} case.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • So if I don't need to do anything else while I'm setting a value, and I never plan to, and nothing is forcing me to have getters/setters (like ASP.NET); there is no real advantage other than it being the normal way of coding a member (much like giving a class the same name as the file - you don't have to, but practically everyone does it)? – BIBD Mar 13 '13 at 04:19
  • 1
    @CodeSlave - It is just a good way of doing it because it creates an interface to the data - thus separating the data and implementation. – Hogan Mar 13 '13 at 04:20
  • @ScottMermelstein - are you sure about that last bit -- I don't think there is anything that requires getters and setters. The only thing I can think of is firing OnChange events and that is just a design and implementation choice -- there a number of patterns that do it a different way (which are used in other languages - eg Java) – Hogan Mar 13 '13 at 04:22
  • 1
    @CodeSlave Yes. If it's just a "public" variable, and nothing's forcing your hand, just make it a public member. You can make semantic arguments about how it's good style to always go through an accessor, but in my opinion, it can be excessive. – Scott Mermelstein Mar 13 '13 at 04:22
  • @Hogan If you're defining a model for MVC.Net, you have to use accessors instead of public members for the server to auto-fill members that are written back. (I've gotten burned by that several times.) Underneath the sheets, the code is using reflection and looking for accessor methods. – Scott Mermelstein Mar 13 '13 at 04:24
  • @ScottMermelstein - But MVC.Net is a library that is forcing you. I could re-write MVC.Net to not require accessor methods and also look for public properties (probably just 2 or 3 lines of code.) Your statement was a little broad. I would agree with the statement : "There are a number of libraries and add-ons that require you to use getter and setter functions." – Hogan Mar 13 '13 at 04:27
  • Ok, I can accept that. All the cool kids are doing it and it does me no harm, therefore I'll do it to :-) – BIBD Mar 13 '13 at 04:30
  • @Hogan I'll quote you on that. :-) – Scott Mermelstein Mar 13 '13 at 04:48
  • Looking at an answer here from ikspeto http://stackoverflow.com/questions/11159438/looking-for-a-short-simple-example-of-getters-setters-in-c-sharp you don't need to put the non public variable with the public one, you can use the public one alone `public int RAM { get; set; }` – barlop Jul 08 '16 at 12:25
  • Yes, that's even shown in the question. I was showing other cases where a gettor and settor could be useful. The trivial case that you mention is functionally no different than a public member, though there are some libraries that require properties instead of members. You can also do `public int myVal{get; private set;}` to hide the settor but have an implicit gettor variable. There's a lot you can do, it all depends on your use case of what you need. – Scott Mermelstein Jul 08 '16 at 12:41
13

Set:

localMyClass.myVal = 42

Get:

int variable = localMyClass.myVal;
6

From the outside, the syntax for accessing getters and setters is indistinguishable from that of accessing variables. Assignments translate into calls of setters, while plain expression uses translate into calls of getters.

In intellisense, the list of getters and setters should open upon placing a dot . after the variable name. Properties should have blue markers to the left of them (as opposed to magenta-colored markers to the left of methods).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
5

You want this

localMyClass.myVal = 42;  

to call the setter

and this

varName = localMyClass.myVal;

to call the getter.

Hogan
  • 69,564
  • 10
  • 76
  • 117
2

Get: var tmp = localMyClass.myVal;

Set: localMyClass.myVal = 2;

BinaryTox1n
  • 3,486
  • 1
  • 36
  • 44