0

I am new to OOP and I think I don't understand static classes.

I want to create a static class Actions and one static method for changing textblock apperance..

Here is my code:

public static class Tools
{
    public enum StatusOption
    {
        Online,
        Offline,
        Warning
    }
}

public class Actions
{
    private SortedDictionary<Tools.StatusOption,SolidColorBrush> StatusColors = new SortedDictionary<Tools.StatusOption,SolidColorBrush>();

    public Actions()
    {
        StatusColors.Add(Tools.StatusOption.Online, new SolidColorBrush(Colors.Green));
        StatusColors.Add(Tools.StatusOption.Offline, new SolidColorBrush(Colors.Red));
        StatusColors.Add(Tools.StatusOption.Warning, new SolidColorBrush(Colors.Orange));
    }

    public void SetStatus(Tools.StatusOption _statusOption, TextBlock _txtBlock)
    {
        _txtBlock.Text = _statusOption.ToString();
        _txtBlock.Foreground = StatusColors[_statusOption];
    }
}

It works, but I have to create several instances of my class, which is IMHO useless.

private void Close_Click(object sender, RoutedEventArgs e)
{
     Actions a1 = new Actions();
     a1.SetStatus(Tools.StatusOption.Offline, StatusTextBlock);
}

private void Open_Click(object sender, RoutedEventArgs e)
{
     Actions a2 = new Actions();
     a2.SetStatus(Tools.StatusOption.Online, StatusTextBlock);
}

I would prefer it just like this:

private void Open_Click(object sender, RoutedEventArgs e)
{
     Actions.SetStatus(Tools.StatusOption.Online, StatusTextBlock);
}

I know, I have to define a static class and static constructor:

public static class Actions
{
  private SortedDictionary<Tools.StatusOption,SolidColorBrush> StatusColors = new SortedDictionary<Tools.StatusOption,SolidColorBrush>();

  static Actions()
  {
    StatusColors.Add(Tools.StatusOption.Online, new SolidColorBrush(Colors.Green));
    // ....
  }
}

The problem is, I can not access to private member StatusColors in static constructor, and I can not create instance of StatusColors.

Any Ideas how to solve it?

Thanks.

Mike Two
  • 44,935
  • 9
  • 80
  • 96
blast3r
  • 69
  • 1
  • 4
  • 8

2 Answers2

5

You can use this code:

public enum StatusOption 
{ 
    Online, 
    Offline, 
    Warning 
} 

public class Actions 
{ 
    private static SortedDictionary<Tools.StatusOption,SolidColorBrush> StatusColors = new SortedDictionary<Tools.StatusOption,SolidColorBrush>(); 

    static Actions() 
    { 
        StatusColors.Add(Tools.StatusOption.Online, new SolidColorBrush(Colors.Green)); 
        StatusColors.Add(Tools.StatusOption.Offline, new SolidColorBrush(Colors.Red)); 
        StatusColors.Add(Tools.StatusOption.Warning, new SolidColorBrush(Colors.Orange)); 
    } 

    public static void SetStatus(Tools.StatusOption _statusOption, TextBlock _txtBlock) 
    { 
        _txtBlock.Text = _statusOption.ToString(); 
        _txtBlock.Foreground = StatusColors[_statusOption]; 
    } 
} 

I made the dictionary static as well and also I put the enum outside the class. You shouldn't use classes for nesting like this, use a namespace if you need to.

aKzenT
  • 7,775
  • 2
  • 36
  • 65
  • It can totally solve the problem. If you want to use unstatic member in any static member, you should declare a variable of `this`. Otherwise, you can change the member to a static one. If your class is static, you'd better put all the member into static. – Kirin Yao Apr 01 '12 at 01:46
0

Make StatusColors static as well. After that, you should read up on the static keyword.

Femaref
  • 60,705
  • 7
  • 138
  • 176