To understand how a property is different from a field, imagine you have derived a class from a
Label
control, to add it the capability to change the foreground color of the shown text. Your code will be similar to the following:
public class ColoredLabel : Label
{
private Color _Foreground = Color.Black;
...
public Color Foreground
{
get
{
return _Foreground;
}
set
{
_Foreground = value;
Invalidate();
}
}
...
}
As you can see, using a property allow you to do more than simply get and/or set the value of a field.
About the error that you are getting, see the documentation of the
Int32.Parse Method (String)[
^]: as it could fails and throws an exception in some cases, you have to add exception-handling to your code to make it robust:
try
{
c1.A = int.Parse(tb1.Text);
}
catch (FormatException e)
{
...
}
catch (OverflowException e)
{
string text = tb1.Text;
text.Trim();
if (text.StartsWith("-"))
c1.A = int.MinValue;
else
c1.A = int.MaxValue;
}
catch (Exception e)
{
...
}