I have some troubles with a class instance and don’t understand the issue. I’m working with colored text in different classes and therefore my idea was to define this colors only one time so changing of them will be much easier.
For the definition of the colors, I specified a class called MyColors. I used System.Windows.Media and defined the RGB colors using the Color class. My idea was to specify each color as privat and add a property that generated the read access to this color, but Visual Studio sends me an error.
namespace MyProject
{
class MyColor
{
private Color myGreen = new Color();
myGreen = Color.FromRgb(0, 255, 0);
#region Properties
/// <summary>
/// Gets my green
/// </summary>
/// <value>my green</value>
static public Color MyGreen
{
get
{
return myGreen;
}
}
#endregion
}
}
The sixth line "myGreen..." generates the mistake. If I change the code to the following one, it works.
namespace MyProject
{
class MyColor
{
#region Properties
/// <summary>
/// Gets my green
/// </summary>
/// <value>my green</value>
static public Color MyGreen
{
get
{
Color myGreen = new Color();
myGreen = Color.FromRgb(0, 255, 0);
return myGreen;
}
}
#endregion
}
}
Can anybody explain me what I’m doing wrong. Looks like I have a wrong understanding of a class.