Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7728557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:44:55+00:00 2026-06-01T05:44:55+00:00

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:44:56+00:00Added an answer on June 1, 2026 at 5:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the poor title,I'm new to OOP so I don't know what is
I'm new to PHP OOP, and I think public $var and __construct() are exactly
I'm new to object-oriented programming, and I'm struggling to think of OOP. What I
I'm somewhat new to OOP programming so it's very likely I'm making some stupid
I am fairly new to OOP and wondered if you could provide some help
I'm new to OOP. Originally I was defining variables and assigning values to them
I am new to JavaScript OOP. Can you please explain the difference between the
I'm new to the OOP paradigm, so there's probably a simple explanation for this
I'm new to programming and some OOP concepts and am not sure what is
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.