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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.