-2

This code gives the error "invalid expression term '}'" on the left curly brace after the return. There is no error when I leave out the curly braces around the return. I thought the curly braces were optional. Google shows nothing for the full phrase "invalid expression term '}'" (at least in the first several pages). No other errors on this routine.

   protected void TestFunction(Int64 varParentID)
    {
        Int64 NextChild = 0;

       NextChild = FindFirstChild(varParentID);
       if (NextChild == -1) {return};
    }
wayfarer
  • 780
  • 2
  • 15
  • 33
  • 3
    Did you forget the semi-colon after the return? The if block doesn't need it. `if (NextChild == -1) {return;}` – Sunny Patel Jun 26 '16 at 00:30
  • Besides having solved this problem, you may want to check this other question, which I think can be of help: http://stackoverflow.com/questions/26803462/why-do-some-lines-not-have-semicolon-in-c – Andrew Jun 27 '16 at 00:58

1 Answers1

0

You need to add ; after return, and you don't need the semicolon after the close curly brace.

   if (NextChild == -1) { return; }
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
MoustafaS
  • 1,991
  • 11
  • 20
  • 1
    We don't answer questions that are just typos, we vote to close them. – DavidG Jun 26 '16 at 01:14
  • 3
    @DavidG: there's nothing wrong with offering the answer to a typo. Yes, the question should and will be closed, and eventually deleted, and any answers along with it. But using an actual answer post to explain the typographical error is fine, if someone actually wants to do that. – Peter Duniho Jun 26 '16 at 04:45
  • 1
    @DavidG This may not be a simple typo. It could very well be lack of knowledge on the language syntax, hence a totally valid question, even if the answer is a single character correction. And given the wording of the OP, I'm tempted to think that's the case instead of a simple "ops, I forgot the semicolon". – Alejandro Jun 26 '16 at 05:01
  • @PeterDuniho I'm happy for the answer to go as a comment (as was done) but other than that it's just not needed. – DavidG Jun 26 '16 at 10:44
  • This was lack of knowledge of coding syntax. – wayfarer Jun 26 '16 at 16:54
  • @wayfarer: _"This was lack of knowledge..."_ -- if you say so. That lack nevertheless led to a simple typographical error, and a question that is unlikely to help anyone else in the future. – Peter Duniho Jun 28 '16 at 02:00
  • @PeterDuniho - it's not a typographical error if I did not know which keystroke to use. I agree that it's unlikely that there will be many people who would be attempting to write C# without such a rudimentary knowledge of syntax, however, I offer myself as one example. Since the error was not found on google, then if stackoverflow decides to exclude it, where does a person like myself turn for the answer? "Go read the manual?" Is that not true for a high percentage of answers on stackoverflow? – wayfarer Jun 28 '16 at 17:26
  • @wayfarer: _"it's not a typographical error if I did not know which keystroke to use"_ -- interesting logic, but completely flawed. The reason you caused a typographical error has nothing to do with _whether_ it is a typographical error. And that's ignoring that you clearly understand, as evidenced by other statements in your program, that C# program statements must be terminated by a semicolon. – Peter Duniho Jun 28 '16 at 17:37
  • @wayfarer: _"Is that not true for a high percentage of answers"_ -- no, but it is true for a high % of questions. That's hardly justification though; it's well known most SO questions are bad questions, but that doesn't mean you should want to be one of the people posting bad questions or leaving them up on the site. – Peter Duniho Jun 28 '16 at 17:37
  • @PeterDuniho I appreciate that this may be difficult to comprehend, however, I did NOT understand the relationship between statements, brackets and semicolons. I tried several different variations and did several google searches before I posted my question. Your position on what constitutes a "bad question" suggests the thought that "people should not use stackoverflow until they have attained a certain level of competence." Who determines what that level of competence is? Does it need to be equivalent to yours? BTW your inference about what I want is insulting. How does SO assess that? – wayfarer Jun 28 '16 at 17:57
  • @wayfarer: _"I tried several different variations and did several google searches before I posted my question"_ -- but apparently didn't read [the documentation](https://msdn.microsoft.com/en-us/library/ms173143.aspx). But frankly, that doesn't even matter that much, as the fact remains that your problem is a **typographical error**. As inferring what you want, be insulted if you want, the fact remains: this is not a useful or good question, yet you want it to remain. It's a logical and obvious inference. – Peter Duniho Jun 28 '16 at 18:14
  • @PeterDuniho - You failed to address the points in my previous comment. So they stand. And since you are repeating yourself, this is no longer a dialog. So I'm satisfied to let other people assess the objective, and subjective, qualities of this exchange. – wayfarer Jun 29 '16 at 00:32