Error cs1010 newline in constant - .net(C#) mislabeling and misleading people

Lionsure 2020-08-22 Original by the website

Sometimes the errors marked by the .net(C#) compiler are very vague. Errors are marked where there are no errors, and errors are not marked where there are errors. This brings great difficulties to troubleshooting. The following is such an error. The error cs1010 newline in constant occurs in the binding output of the foreground page(aspx page), the specific error is as follows:


Compiler error message: cs1010 newline in constant

<a href="<%# Eval(Id") %>&n=<%# Server.UrlEncode(ToString(Eval("ClassName"))) %>"><%# Eval("ClassName") %></a>


The error prompt is marked with a red squiggly line at the <%# of <%# Eval("ClassName") %>. No matter how you look at it, there is no error at all. Such a prompt is completely misleading. If you stay here to find the error, you will always no error can be found; one thing is certain, the error must have occurred here and will not be in the code behind, because the code behind will not be marked with a red wavy line in the foreground.

A misleading error like this, how should we find it out? In fact, it's very simple. Since it can be determined that the error occurred at the place marked with a wavy line, if the code is not long, you can carefully check it from beginning to end, so that you can find the error quickly; if the code is relatively long, you can find it use the red wavy line as the starting point to check forward or backward, so that you may be able to find it quickly.

The above code is only one line shorter. You can check the whole line again. After careful inspection, you can find: A double quotation mark(") is missing before Id in <%# Eval(Id") %>. After adding the double quotation mark, the problem is solved.

It can be seen from the above results that the error is finally found. Due to the diversity and complexity of errors, the compiler cannot accurately indicate the specific location of error every time. After all, people are more flexible than it. Sometimes, it just probably prompts the error, sometimes even throws a misleading prompt. These require us to play our subjective initiative, carefully analyze and investigate carefully, so as to quickly find the error.