using variable strings in switch case statements

public string password

switch(inputField.text)
{
| case password:
| debug.Log("correct");
| break
| default:
| inputField.text = " ";
| break
{

I’m trying to use a public string that can be changed by a user to do a password check. However, I am getting the error: CS0150 A constant value is expected. Is there still a workaround to allow for the use of variable strings or am I better off looking for another way to do this?

Nothing wrong with just an if-else statement.

3 Likes

You need double quotes on the case.

The basics have already been explained above.

An advanced topic for the future is to never compare passwords directly. Store passwords SHA or MD encoded. Then what comes from the user must also be encoded and then both values are compared.

One of the things that makes switch statements faster is that it doesn’t have to evaluate variables to make the switch. As spiney199 points out there is nothing wrong with an if-else but more importantly in this case even if it worked a switch statement with essentially one case isn’t optimal. It can only be true or false.

Try making it a separate method like IsPassword and pass two strings (not one particular inputField) and have it return true or false. Don’t change the inputField inside the method it limits reusability. And I wouldn’t set it to a single blank character regardless.

Sadly, C# lacks elaborate pattern-matching capabilities (hello Prolog!)

I mostly use switches over enum types – and even then, it’s only really when I want to have several cases run the same logic:

switch(state) {
  case State.Idle:
  case State.Sleeping:
  case State.Fleeing:
    foo();
    break;
  case State.InternetArgument:
    bar();
    break;
}

Well, the user should still send the cleartext password. The point of storing a hash is that you can’t easily figure out what the original password was. If the user sends a hashed password, then that is the password!

(and i’d better not catch anyone using MD5 to hash passwords…)

Maybe because of my poor English we didn’t understand each other.

I talked about storing an encoded password, and what comes from the user’s input (readable text) will be encoded in the program. Then compare the two values (both encoded at this stage).

Ah, yeah, that’s reasonable! I misread your post as saying the user sends the hashed password (possibly over the internet).

Latest versions of C# have some pattern matching. Not quite as elaborate as many of functional languages, but more than nothing. Of course it should only be used when it makes sense, not for the simple stuff where simple if statement works as well and is easier to read.

class Block {
    public int v=0;
    public Block left=null;
    public Block right=null;
}
class B2 : Block { }

public string x = "x1";
public string y = "x2";


public int Foo3(object val)
{
    switch (val) {
        case "abc":
            return 0;
        case "abcd":
            return 1;
        case (32, 42):
            return 21;
        case (4, 2, "abc"):
            return 22;
        case (6, 5, string x) when x.StartsWith("prefix"):
            return 23 + x.Length;
        case string v when v == x:
            return 2;
        case float v when v > 4.2:
            return 3;
        case Block {v: 32, left: Block {v:7, left: null, right: B2 lr}, right: var r} when r == null:
            return lr.v;
        case >=2 and <23:
            return 5;
    }
    return -1;
}
1 Like