G+_Matthew Reardon Posted October 1, 2014 Share Posted October 1, 2014 Episode 33 question I don't understand the (get, set) in the following line of code. Is this a variable or a function? Both? public CurrencyCodes CurrencyCode { get; set; } Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted October 1, 2014 Share Posted October 1, 2014 That's just a shortcut way of telling the compiler to automatically create get and set methods for the class that modify the property, which is a common thing to do with class properties. Link to comment Share on other sites More sharing options...
G+_Joe C. Hecht Posted October 1, 2014 Share Posted October 1, 2014 It is a declaration of a public "property" variable. Variables (properties) are often accessed via a Get method, and initialized via a set method. Like Lee Crocker said, the compiler is adding those methods automatically. Setter methods allow you to do things like range check input, and perform other tasks For example, setter method for the BorderWidth of a Window control would set the value, and if the value changed, would then update (redraw) the window using the new border width. Getter methods usually require just a simple read, but can do more if required. TJoe Link to comment Share on other sites More sharing options...
G+_Matthew Reardon Posted October 2, 2014 Author Share Posted October 2, 2014 Thanks Joe Hecht and Lee Crocker . Link to comment Share on other sites More sharing options...
G+_Louis Maresca Posted October 2, 2014 Share Posted October 2, 2014 Great stuff guys! There is an older post done on StackOverflow that explains what the compiler is doing by using examples: http://stackoverflow.com/a/341364 Link to comment Share on other sites More sharing options...
G+_Rik Schreurs Posted October 2, 2014 Share Posted October 2, 2014 Very cool! I like the form public int Property { get; private set; } where the property is only writable from a method in the same class. Makes it less likely for other parts of the program to mess up an object. Link to comment Share on other sites More sharing options...
Recommended Posts