David DeMar
David DeMar

Reputation: 2660

Using an object's string variable in a switch statement

I'm trying to figure out why I'm getting this error:

    switch (token.type.GetName()) 
    {
        case PascalTokenType.BEGIN.name: // ERROR: A CONSTANT VALUE IS EXPECTED
        {
            CompoundStatementParser compoundParser = new CompoundStatementParser(this);
            statementNode = compoundParser.parse(token);
            break;
        }
    }

GetName() and name both return strings. This is what the object looks like:

    public static readonly PascalTokenType BEGIN = new PascalTokenType("BEGIN");

I know I could get around this problem by changing it to an if statement but when this is complete I'm going to have at least 20 conditions so I'd rather not. I'm supposing it's because BEGIN.name isn't assigned until runtime?

Upvotes: 0

Views: 416

Answers (1)

Killnine
Killnine

Reputation: 5880

Yeah, you have the right idea. The problem is that this value could change at runtime, which the compiler doesn't like.

Upvotes: 3

Related Questions