I have created a Boolean variable called 3D_pressed and have got the error "error CS1519: Unexpected symbol `3' in class, struct, or interface member declaration". How do I solve this? This is in unity using the the programming language C#.
C# variable name creates error
Paste your actual code causing the error.
Edit: In this case, it's because your variable starts with a number -- a variable name cannot start with a number.
For future reference, though, please always include both the code causing the error as well as the complete error message.
8 minutes ago, Lactose said:Paste your actual code causing the error.
Edit: In this case, it's because your variable starts with a number -- a variable name cannot start with a number.
For future reference, though, please always include both the code causing the error as well as the complete error message.
Sorry for that the code is attached in this quote. Why can't a variable start with a number? Is it possible to add a number to a variable name in unity using C#?
public bool 3D_pressed; error = Assets/Scripts/Main_UI_manager.cs(14,15): error CS1519: Unexpected symbol `3' in class, struct, or interface member declaration
8 minutes ago, davejones said:Sorry for that the code is attached in this quote. Why can't a variable start with a number? Is it possible to add a number to a variable name in unity using C#?
public bool 3D_pressed;error = Assets/Scripts/Main_UI_manager.cs(14,15): error CS1519: Unexpected symbol `3' in class, struct, or interface member declaration
This is because in C# you are not allowed to start variable names with numbers. Change it to something else like: game_3D_pressed;
2 minutes ago, Rutin said:Why can't a variable start with a number?
Because the compiler could confuse it with a numeric literal.
Just as an aside, C/C++ has this same rule; no variable, class, struct or function (C#'s member) can start with a number. Not sure about other programming languages.
C# will also let you start an identifier with just the underscore. So, if you want the first alphanumeric character to be a digit, you can use this:
bool _3D_pressed; You'll probably have to listen to some howling from your colleagues if you do this, though. Using the underscore as the first character in an identifier is typically reserved for special purposes (like storing the "backing" value of a property). So, mostly, I mention this for the purpose of advising against doing it. Rutin's suggestion is really the better alternative. Also, you might pick up a copy of "The Elements of C# Style." It's an easy to use reference on the conventions most commonly used to guide your choices in writing C# code.
13 hours ago, Stevens R. Miller said:C# will also let you start an identifier with just the underscore. So, if you want the first alphanumeric character to be a digit, you can use this:
bool _3D_pressed;You'll probably have to listen to some howling from your colleagues if you do this, though. Using the underscore as the first character in an identifier is typically reserved for special purposes (like storing the "backing" value of a property). So, mostly, I mention this for the purpose of advising against doing it. Rutin's suggestion is really the better alternative. Also, you might pick up a copy of "The Elements of C# Style." It's an easy to use reference on the conventions most commonly used to guide your choices in writing C# code.
Thank you for your book recommendation. I will purchase the book you have recommended and make it a go to reference on a daily basis where I am coding. So when I am coding I'll refer to it and make annotations notes on what I feel is optimum.
One reason for this is to prevent ambiguities. Numeric literals can have a suffix to indicate their type, e.g 42l. Without a rule saying that identifiers don't start with a number, it may not be clear if such tokens were identifiers or literals.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.