Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Burichan 17 posts 129 karma points
    Aug 25, 2017 @ 09:19
    Burichan
    0

    Hi guys, later this morning I was trying to write down the following loop using a variable named "event".

    @foreach (var event in myEvents) { 
        <span>@event.GetPropertyValue("title")</span>
    }
    

    All I got was an error message from the compiler stating that it was expecting a '}'. Does the word "event" act as a keyword? Because after renaming it, everything worked fine.

    Peace.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Aug 25, 2017 @ 09:42
    Dave Woestenborghs
    1

    Hi Burichan,

    I think this beause event is a reserved word in .NET

    Maybe you can try @@eventt.GetPropertValue("title") or @(event.GetPropertValue("title"))

    That should fix it.

    Dave

  • Burichan 17 posts 129 karma points
    Aug 25, 2017 @ 09:54
    Burichan
    0

    Hmm, double '@' sign? Never came up with a syntax like this before. Compiler still doesn't let me use the word "event", but it's alright. I can live w/o it. But it felt strange due to the "expecting {" message. It convinced me I was blind for a second... Hahaha!

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 25, 2017 @ 10:28
    Dan Diplo
    1

    Yes, "event" is a reserved keyword in C#. Whilst you can work around it - like Dave says - you'd be best to avoid using any of these are variable names to avoid cofusion: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/index

Please Sign in or register to post replies

Write your reply to:

Draft