You should think about the naming of the property a little more, because one property can have both a getter and a setter. Consider the following:
public class MyClass
{
private Button btnMyButton;
public Button getMyButton
{
get{ return btnMyButton; }
set{ btnMyButton = value; }
}
}
// in some other class
void ChangeActiveButton(Button newButton)
{
MyClass theThing = GetTheThing();
// This doesn't read well...
theThing.getMyButton = newButton;
}
When you implement property getters and setters, don't prefix the name with 'get' and set'. (To many developers, the words 'get' and 'set' in a method or function imply that the code has to go off and do some work to complete the getting or setting, rather than simply return or assign a value that is already to hand.)
public class MyClass
{
private Button btnMyButton;
// Note that the property just has a name, no prefix.
public Button MyButton
{
get{ return btnMyButton; }
set{ btnMyButton = value; }
}
}
Also note that you can make property getters and setters private
even though the property itself is exposed outside the class.
public Button MyButton
{
get{ return btnMyButton; }
private set{ if(null == btnMyButton) btnMyButton = value; }
}
This provides the MyClass
with priveliged access to the setter, which can be used to implement any property-specific assignment rules.
You can also use Auto-Implemented Properties, without the additional member variable.
public Button MyButton { get; private set; }
Properties in c# are great. Use them wisely and it will help you create better structured, more easily maintainable code.