A “property” gives you access to a “property” of a “property.” You probably used properties before, now you learn how to make them yourself.
You’ve probably already seen the “property editor” in Visual Studio, especially if you’ve created a WinForm app. Here you can view and customize all kinds of properties of a Form, Button, TextBox and many other .
If you request the Text of a TextBox from code, or you specify a value, use properties.
string input = TextBoxInput.Text;
// dan een aantal berekeningen (weggelaten)
result = ...
// tot slot de uitkomst in een label terug:
LabelOutput.Text = result;
Suppose you have a Stopwatch class, Then this one could have a private field seconds. Do you want this field to be read only by other code, you can create a property for this. The convention is that fields are written with lowercase letters; Properties are started with a capital letter. See the example below:
class Stopwatch
{
private int seconds; // Field
public int Seconds // Property
{
get { return seconds; } // Getter
}
}
Below are a few ways in which this class may not be used.
Stopwatch sw = new Stopwatch();
int tijd1 = sw.seconds; // Mag niet, omdat field seconds private is.
int tijd2 = sw.Seconds; // Mag wel (hoofdletter) omdat de property public is.
sw.Seconds = 10; // Mag niet (geen setter)
Another possibility is to set a field in a certain way. For example, we could make the stopwatch adjustable with minutes:
class Stopwatch
{
private int seconds; // Field
public int Seconds // Property
{
get { return seconds; } // Getter
}
public int Minutes // Property
{
get { return seconds / 60; } // Getter
set { seconds = value * 60; } // Setter
}
}
Stopwatch sw = new Stopwatch();
sw.Minutes = 5; // Instellen in minuten
int tijd = sw.Seconds; // Uitlezen in seconden (300)
If you want to read more about properties, you can find a good explanation on MSDN (for now ignore the more extensive example under the example header). MSDN on properties