2

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?

3
  • I think you have a conceptual misunderstanding about what enums are. They are a collection of closely related identifiers that can have a specific value. A subset of numbers is a reasonable type, but it isn't an enum (and isn't directly supported in C#). Commented Sep 1, 2016 at 15:31
  • 2
    Why don't you use a simple int property GuestCount instead of an enum? Commented Sep 1, 2016 at 15:36
  • 2
    You are so lucky, that your question was not shot into head by some super user with all the questions in the JavaScript or HTML ;). But as they were not around, luckily, you got two good answers:). Commented Sep 1, 2016 at 15:41

4 Answers 4

13

You probably want this:

public enum guests { One = 1, Two = 2 } 
Sign up to request clarification or add additional context in comments.

1 Comment

It's worth noting that OP can cast the enum value to int and use ToString to get the text.
1

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.

Comments

0

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.

Comments

0

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'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.