user979331
user979331

Reputation: 11961

Public enum with int values

I have defined a public enum like so:

public enum guests
{
    One,
    Two
}

What I am trying to do is to have it like the following

public enum guests
{
    1,
    2
}

but I keep getting this error when I try that:

Identifier expected

Can I set int instead of strings?

Upvotes: 2

Views: 704

Answers (4)

Sagar
Sagar

Reputation: 464

Identifier is a name that can be used to identify variable, method name, member name or any user defined name uniquely. Since identifier rule says that it can not be started with a digit and user defined enums contains identifier, it gives a compile time error as 'Identifier Expected'

Upvotes: 0

KuboK
KuboK

Reputation: 68

An enumeration is a set of named integer constants. So each symbol inside the enum stands for an int, one greater than the symbol before. Defaultly, the value of the first enum symbol is 0.

enum Days{Mon, Tue, Wed, Thu, Fri, Sat, Sun}

So Mon = 0, Tue = 1, etc.

Upvotes: 0

Gene Olson
Gene Olson

Reputation: 930

You could simply Johan's answer to:

public enum guests
{
     One = 1, Two
}

And get the same result. In your original question you would have found One=0 and Two=1. By specifying the value of the first element, all subsequent elements then increment by 1.

Upvotes: 1

Johan
Johan

Reputation: 929

You probably want this:

public enum guests
{
    One = 1,
    Two = 2
}

Upvotes: 13

Related Questions