Advertisement

C# variable name creates error

Started by July 20, 2018 01:51 PM
7 comments, last by rip-off 6 years, 4 months ago

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#. 

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.

Hello to all my stalkers.

Advertisement
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.

Programmer and 3D Artist

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.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

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.

"There never was a Democracy Yet, that did not commit suicide." John Adams, 1814

Advertisement
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.

This topic is closed to new replies.

Advertisement