1

I am trying to remake my C#/WPF take on the Simon game by making it more clean and use classes now that I finally understand classes. Before I ask my question - here is some sample code from my old version as a reference.

  public MainWindow()
        {
            InitializeComponent();
            RedButton.IsEnabled = false;
            BlueButton.IsEnabled = false;
            GreenButton.IsEnabled = false;
            YellowButton.IsEnabled = false;
        }

        //Use TextBlock clearing to say "Watch the pattern", "Your Turn" and then an empty box between games
        private void StartButton_Click(object sender, RoutedEventArgs e)
        {

            _IsError = false;

            RedButton.IsEnabled = true;
            BlueButton.IsEnabled = true;
            GreenButton.IsEnabled = true;
            YellowButton.IsEnabled = true;

            StartButton.IsEnabled = false;
            if (randomPattern.Count == 0)
            {
                randomPattern.Add(random.Next(0, 4));
                randomPattern.Add(random.Next(0, 4));
            }
            StatusBox.Text = "Watch the Pattern.";
            ShowPattern();
        }

So, my question is: Would it be good practice for me to use IsElement as a property/attribute of class "Buttons" instead of writing all of their statements out individually? And if I should add them to the class, I think my next question comes down to - how would I implement it into the class? I know simply typing only "IsEnabled = <T/F>" won't do anything.

EDIT: To show you what I mean:

Should I have IsEnabled as a class property like such:

class Button {
     IsEnabled = false;
}

var greenButton = new Button()

//Some code that lets the player start the game by clicking a button

greenButton.IsEnabled = true;

Or should I keep my IsEnabled statements outside of a class like in my old version of the code? Note - my old version of the code, there are no classes that I made - only functions.

  • Welcome to Stack Overflow! It looks like this question is missing some context- you mention `IsElement` but don't reference that anywhere in code. Could you please elaborate so that we could better assist you – Dillanm Aug 25 '22 at 08:31
  • Dillanm, I believe you are asking for clarification on the question itself. So, I use IsEnabled to make sure players can't click the buttons in my Simon game when the game is not in a session. So, once the player clicks 'Start Game', all of the buttons are enabled/clickable. So, I am just asking if I make a 'Button' class for all of the buttons and each button will be an instance of 'Button' - should I use IsEnabled (for each button) as a property/attribute of class 'Button'? Or should I continue to hardcode those IsEnabled value changes outside of a class when needed? Does that help? – RyanberryPi Aug 25 '22 at 09:11
  • Actually - see my edited post, Dillanm. – RyanberryPi Aug 25 '22 at 09:18
  • Oh I see what you're trying to say now- having a base class with common properties would be more clean and easy to understand. In terms of implementation, [MSDN](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties) is your friend. The class would look something like `public class Button { public bool IsEnabled { get; set; } }` and your XML would hook into that property ( e.g. `greenbutton.IsEnabled`). – Dillanm Aug 25 '22 at 09:30
  • The only problem I can see is that you are going to be moving from using WPF in a 'code behind' manner (which is more accessible to beginners but more akin to old winforms) to using it in an MVVM pattern (which is better suited to object oriented programming). A good tutorial for WPF with MVVM is [here](https://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/) – Dillanm Aug 25 '22 at 09:32
  • Dillanm, I am not sure I understand what your second comment is saying - and I am not sure what MVVM is. I will look into MVVM. But for the first comment, is that getter and setter going to allow me to change the value of IsEnabled for that class? I understand it is a parameter, but I am not sure why it is a parameter. – RyanberryPi Aug 25 '22 at 09:47
  • MVVM is probably more complicated than you require right now so was just mentioning it as you will invariably come across it at some point when working with WPF. For your last question- yes, the getter and setter will allow you to get or set the `IsEnabled` property on an _instance_ of a button. E.g. if you have `var greenButton = new Button();` then you can do `greenButton.IsEnabled = true;` or `var greenEnabled = greenButton.IsEnabled;` – Dillanm Aug 25 '22 at 10:15
  • 1
    Your question still refers to `IsElement`, but you haven't mentioned that anywhere else. If you meant `IsEnabled` (which you talk about in the comments), please edit your question to refer to that instead. It's very confusing to have a question asking about one thing (in both the title and the body) when you actually mean a different property. – Jon Skeet Aug 25 '22 at 10:51
  • Feel free to ask any question. If you feel that my reply is helpful, then you can upvote or mark my reply as an answer to simplify the future search of other users. [How does accepting an answer work?](https://meta.stackexchange.com/a/5235/309682) – StepUp Aug 30 '22 at 04:48

1 Answers1

0

IsEnabled should be placed into Button class as it is property or feature of button.

public class Button {
     public IsEnabled { get; set; }
}

The above class has high cohesion as it is focused on what it should be doing. It has only methods/properties, fields relating to the intention of the class.

StepUp
  • 36,391
  • 15
  • 88
  • 148