-2

I'm trying to make a super simple program where you use 10 different buttons to write in a code (kind of like the code to the door of an appartment complex). All the buttons have this click event:

    private void number_click(object sender, EventArgs e)
    {
        var button = (sender as Button);

        if (code.Length == 4)
        {
            code.Remove(0, 1);
        }

        switch (button.Name)
        {
            case "button_1":
                code += "1";
                break;

            case "button_2":
                code += "2";
                break;

            case "button_3":
                code += "3";
                break;

            case "button_4":
                code += "4";
                break;

            case "button_5":
                code += "5";
                break;

            case "button_6":
                code += "6";
                break;

            case "button_7":
                code += "7";
                break;

            case "button_8":
                code += "8";
                break;

            case "button_9":
                code += "9";
                break;

            case "button_0":
                code += "0";
                break;
        }

        label1.Text = code;
    }

I'm simply trying to make so the number the user presses get added to the code string. When there length of the string reaches 4 it is supposed to remove the first character so that there is never more than 4 characters in the string. For some reason this doesn't seem to work. What am I doing wrong?

Machavity
  • 30,841
  • 27
  • 92
  • 100
MacA
  • 15
  • 6

3 Answers3

0

If you want to update the code variable you can use Substring method.

if (code.Length == 4)
{
    code = code.Substring(1);
}
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
0

Your problem is caused by the fact that System.String instances are immutable. This means you can't modify the string code points to, but you can make it point to another string. The Remove method of System.String doesn't actually remove anything, it just creates a new string without the unwanted characters and returns a reference to it. This means that instead of

code.Remove(0, 1);

You want:

code = code.Remove(0, 1);
Gur Galler
  • 820
  • 9
  • 21
0

The immediate cause of the error is that string is immutable and so

code.Remove(0, 1);

computes result string and throws it away. You should assign the result string back:

...
code = code.Remove(0, 1);
...

You can get rid of long switch case and obtain digit from the button's name:

private void number_click(object sender, EventArgs e) {
  var button = (sender as Button);

  // Add new digit to the right
  code += button.Name[button.Name.Length - 1].ToString();

  // Ensure code is at most 5 digits long by removing digits from the left
  code = code.Substring(Math.Clamp(code.Length - 5, 0, code.Length)); 
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Ah okay I understand. Thank you very much! And using the name of the button to get the number was very clever. I did however not understand that last part with substring, math.clamp etc. Whould you mind explaining further how that workls? – MacA Jul 31 '21 at 15:31
  • @MacA: `Math.Clamp(x, min, max)` returns `x` when it within `min..max`, `min` when `x` is too small (x < min) and `max` when `x` is too large (x > max). Here I put `Math.Clamp` to ensure that `Substring` has argument in valid range – Dmitry Bychenko Jul 31 '21 at 15:37