C# Tutorial - 2. invalid token and expression term
Welcome to the second lesson of my Visual C# Tutorial. In this post, we are going to go through some of the most common mistakes that you might have when you first start writing applications with Visual C#.
Visual Studio is kind of user-friendly so that whenever you try to run your applications with some syntax errors or wrong capitalization, it informs you by opening up the following window.
When you see this error dialog opening up in the middle of your Visual Studio, it means that you have misspelling, wrong capitalization or syntax errors in you codes. To take a close look at what causes problems, let's just click the "No" button. Then you will get the little box appear at the bottom of your Visual Studio.
It basically contains a list of errors that occur in your codes. At this stage where we have kind of simple codes for our application, It won't be that hard to identify and fix what went wrong by looking at the list and description. Let's get through one by one.
Step 1. Invalid token
As you can see, i have this error message that "Invalid token "(" in class, or interface member declaration". It is probably because i put my codes in the wrong places. As you have learned, a block of code is defined by two curly braces "( )", and different commands must be in the right types of code blocks. We are going to go through it in detail later, but for now, it simply refers to that some of your code is in the wrong block of code.
Tips. Tokens are the most basic elements of your code. They can be categorized into five types, Keyword, Identifier, Operator, Separator and Literals. You can simply think of them as the basic block building your C# codes.
As you can see, those two lines of code that we wrote in the last tutorial are not between the curly braces of static void Main(string[] args) to which they should belong. So you can just move your codes around in between upper curly braces. It will fix your errors.
Step 2. Semi-colon expected
If you have this message "Semi-colon expected" as following, all you have to do is just put semi-colon at the end of your error lines.
You can also see the red line in the place where you should put semi-colon. This red line tells you that you typed something wrong or missed something that is against syntax in C#.
C# code must end with semi-colon. It is actually separator which is one of the token elements. Basically it separates each code just like period does with a sentence in our languages.
Step 3. Invalid expression term
When you don't put double quotes around literals which are the message "Hello World" in this case, you would get this error message "The name 'Hello' does not exist in the current context". Since we want the message "Hello World" written to the screen, we have got to note that these words represent a string literal that is printed into the screen by double quotes. We are going to learn more about string literal and string later.
Similarly, you might have this issue with keyword or method name if you misspelled or did not capitalize.
As you can see, i have lowercase "c" in the word console keyword and lowercase "r" in the word readLine.
In this case, we all know that there are definition for Console and ReadLine in Visual C#. So we should pay attention to the spelling or capitalization of the those words when this issue occurs. It is just tiny mistakes that you could fix right away, but i just want to remind you that accuracy and capitalization are really important as far as C# programming concerns.
Up to this point, we have looked at some of the common mistakes in relation to accuracy, capitalization and syntax in C#. As you learn more about C# programming and get used to write codes, these problems would occur less often.
Post a Comment