سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top