kvambaam kvambaam - 3 months ago 11
ASP.NET (C#) Question

Replace a complex if else statement

I am looking for a clean and effective way of accomplishing this (See picture). I want to stack different buttons side by side depending on if they are visible or not. I started out by using if else statements but this way of doing it got fast very complicated and ineffective.

enter image description here

  • Case 1: Only one button is visible. Align is center.

  • Case 2: Displaying another button. Two buttons are visible.

  • Case 3: Displaying an OFF button. Three buttons are visible.

  • Case 4: Hiding ON and Close. OFF is center.

  • Case 5: A fourth new button is added to the stack/page.

    private int startX { get; set; }
    private int width { get; set; }
    private int gap { get; set; }

    private Boolean showOnBtn { get; set; }
    private Boolean showOffBtn { get; set; }
    private Boolean showCloseBtn { get; set; }

    public Buttons(int startX, int width, int gap)
    this.startX = startX;
    this.width = width;
    this.gap = gap;

Example of my if else attempt:

width = 600px;

Button onBtn = new Button();

Private void Update(){

if (showOnBtn){

onBtn.startX = 242;
onBtn.width = 116;
onBtn.gap = 0;

if (showOffBtn || showCloseBtn) {

onBtn.startX = 126;
onBtn.width = 116;
onBtn.gap = 113;
etc etc…



In the abstract, you must have five totally different panels.

A basic pattern in software engineering is that if you have to turn "only one one", you have a routine that turns all of them off, and then turn that one on.

Abstract code:

function TurnAllOff()
 for i = 1 ... 5 Buttons[i].Hide();

function ShowThisOne(int i)

I have utterly no clue if you are working in Unity, the www, or whatever. But you will have to have precisely the above code (just in your local syntax).

That's all there is to it. You can not generalize the layout, in your case.

Have five different perfect layouts for the five different button systems.

Use the pattern above to choose one at any time.