locked
Identifier expected; 'operator' is a keyword RRS feed

  • Question

  • User457850011 posted

    I am using MVC 5 Razor view to display an item and getting the above error: Identifier expected; 'operator' is a keyword

    below is my code.

    @foreach (var item in Model) {@item.operator}

    Tuesday, May 26, 2020 5:03 PM

Answers

  • User753101303 posted

    Could you confirm you tried :

    @foreach (var item in Model) {@(item.@operator)}

    I'm using a List<Test> model with two entries and using "A" and "B" for the operator value. It does show AB on my view. This is really MVC or WebPages? Not sure what could make the difference it still fails on your side and which the same error message.

    Edit:

    public string @operator { get; set; }

    I am also able to modified my class to use _operator

    No, @operator is how to name a property operator despite being a C# keyword. From the link I posted earlier :

    "Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier, but if is not because if is a keyword."

    AFAIK this is just part of the syntax if using System.Reflection you should see the property name is really just "operator".

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2020 2:34 PM

All replies

  • User1120430333 posted

    expected; 'operator' is a keyword

    @foreach (var item in Model) {@item.operator}

    There are certain reserve words that you cannot use that are part of the computer language being used, and operator  is one of those reserved words apparently. Name it operatorx or something other than operator. :) 

    Tuesday, May 26, 2020 6:04 PM
  • User457850011 posted

    Hi

    Thanks for your response. The name operator is a column name from system database--dbo.sysssislog that I am trying to display so I cannot change the name

    Tuesday, May 26, 2020 6:07 PM
  • User753101303 posted

    Hi,

    This is a C# keyword: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

    When using them you have to use @keyword. As this is in the Razor page I believe you have to escape the @ character by using @@. So try {@item.@@operator}

    Or if you prefer change this identifier to something else?

    Tuesday, May 26, 2020 6:08 PM
  • User457850011 posted

    It didn't work so I guess I have to omit it altogether

    Tuesday, May 26, 2020 6:37 PM
  • User475983607 posted

    This is a bit odd because it is not possible to have a property named "operator".  Operator with a capital "O" is valid.

        public class SimpleVm
        {
            public string Operator { get; set; }
        }

    Try...

    @foreach (var item in Model) {@item.Operator}

    Tuesday, May 26, 2020 6:45 PM
  • User1120430333 posted

    denkyira

    Hi

    Thanks for your response. The name operator is a column name from system database--dbo.sysssislog that I am trying to display so I cannot change the name

    But you are using a class in the solution that has public properties. The public property in the class is operator.

    You can use a partial class to rename the property to something else but still keep the original property name so that it doesn't break other code dependent upon the original property name

    https://csharp.net-tutorials.com/classes/partial-classes/

    First person.cs
    public partical class Person
    {
          public string Firstname {get; set;)
          //other properties
    }
    
    second person1.cs
    
     public partical class Person
    {
          public string Firstnamex {get; set;)
          // other properties
    }

    Tuesday, May 26, 2020 7:05 PM
  • User753101303 posted

    I tried and it worked for me by adding parenthesis.

    @foreach (var item in Model) {@(item.@operator)}

    Not sure how you get data or how you created your model class. An alias column could work. If you declared the class you should have used @ already. The class I used for testing is :

      public class Test
      {
         public string @operator {get;set;}
      }

    Tuesday, May 26, 2020 7:08 PM
  • User-474980206 posted

    its a little confusing. in c# the @prefix is used when reserved words are used as an identifier. in razor its used to toggle code mode from html mode. you only need @@ to produce an @ for html., once in c# mode you just use it, also because you are in c# mode, you need to toggle to html mode:

      @foreach (var item in Model) { item.@operator;  /* @item is not required because item is not a reserved word. because its a a c# statement ; is required */ }

    but I assume you want to display the value as html, so you need to exit c# mode first (@:, or <text>)

      @foreach (var item in Model) {@:@item.@operator}

    or

      @foreach (var item in Model) {<text>@item.@operator</text>}

    because the @: or <text> changed c# mode to html (razor) mode  the @item is to exit html mode to C# expression mode. @operator is required because its a reserved word.

    Tuesday, May 26, 2020 7:10 PM
  • User457850011 posted

    Hi Bruce

    Thanks but I have tried all the solutions above and none of them works. Getting the same results. I will omit it for now but still look for an answer to educate myself

    Tuesday, May 26, 2020 9:49 PM
  • User1120430333 posted

    Hi Bruce

    Thanks but I have tried all the solutions above and none of them works. Getting the same results. I will omit it for now but still look for an answer to educate myself

    Are you saying that a partial class doesn't work on the model/database class/object?

    I cannot believe that a viewmodel object wouldn't work  when you map the database model object/class over to a viewmodel class/object that is strong typed to the view and change the name of the property in the VM. You are leaving the persistence model object, the database object,  behind and sending in a viewmodel object into the view.

     https://en.wikipedia.org/wiki/Persistence_framework

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    Myself, I never send a persistence model class/object into the view never. I always send in a VM that  strong typed to the view and map back and forth between a VM object and persistence object using the persistence object only with the data persistence technology. 

    Tuesday, May 26, 2020 10:46 PM
  • User1686398519 posted

    Hi,  denkyira

    I use database first to generate the class, "operator" is preceded by @.

    public string @operator { get; set; }

    You can modify your page code like this.

    @(item.@operator)

    Best Regards,

    YihuiSun

    Wednesday, May 27, 2020 10:06 AM
  • User457850011 posted

    Good Day YihuiSun

    Yes this will work because you used 

    public string @operator { get; set; }

    I am also able to modified my class to use _operator and it works. I still want to know how do we use the operator without modifying it?
    Wednesday, May 27, 2020 2:13 PM
  • User475983607 posted

    _operator and it works. I still want to know how do we use the operator without modifying it?

    As suggested above use a capital "O" in Operator.

    public string Operator { get; set; }

    Wednesday, May 27, 2020 2:15 PM
  • User-474980206 posted

    Good Day YihuiSun

    Yes this will work because you used 

    public string @operator { get; set; }
    
    I am also able to modified my class to use _operator and it works. I still want to know how do we use the operator without modifying it?

    like many languages c# has a set of reserved words of which operator is one. In c#, if a reserved word is used as an identifier It must be preceded with a @ everywhere its used in code. The most common case of this in mvc is the reserved word class which must always be quoted when used When declaring html attributes.

    Wednesday, May 27, 2020 2:21 PM
  • User753101303 posted

    Could you confirm you tried :

    @foreach (var item in Model) {@(item.@operator)}

    I'm using a List<Test> model with two entries and using "A" and "B" for the operator value. It does show AB on my view. This is really MVC or WebPages? Not sure what could make the difference it still fails on your side and which the same error message.

    Edit:

    public string @operator { get; set; }

    I am also able to modified my class to use _operator

    No, @operator is how to name a property operator despite being a C# keyword. From the link I posted earlier :

    "Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier, but if is not because if is a keyword."

    AFAIK this is just part of the syntax if using System.Reflection you should see the property name is really just "operator".

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2020 2:34 PM