8

this may be a simple question for people, but I can't see why this is occurring. here is my code 1st:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameCore
{
    public class PlayerCharacter
    {

        public void Hit(int damage)
        {
            Health -= damage;

            if (Health <= 0)
            {
                IsDead = true;
            }
        }

        public int Health { get; private set; } = 100;
        public bool IsDead{ get; private set; }

    }
}

now Visual studio is giving the Invalid token error on the assignment sign (=) (as per the title), and I can not see why. can anyone shed light on this please?

What I'm trying to do is set the int of Health to 100, and each time a character takes damage, then Health is decreased. Thanks all.

I'm using Visual Studio 2013 v12.0.40629.00 update 5

Graeme Phillips
  • 131
  • 1
  • 1
  • 10
  • @SimonKarlsson In reality, a death check would probably be performed after each hit anyway. Personally I'd just make Hit return it as boolean right away to avoid another call. Anyway, optimizing his game isn't the question here. – Nyerguds Mar 23 '16 at 14:36
  • thanks guys, I will consider that, this is a simple class for experimentation with specFlow... I will change the public bool at a later date.. just wanted to get things going atm. thanks again, G. – Graeme Phillips Mar 23 '16 at 14:39
  • @Nyerguds You are correct we shouldn't however my point was rather about ensuring correctness then optimizing. Imagine the scenario where he adds a revive method, he would then here also need to change the value of the `bool` `IsDead`. My point was just that `IsDead` is more of a check then an actual value. :) – Simon Karlsson Mar 24 '16 at 09:05
  • @GraemePhillips You should [accept](http://meta.stackexchange.com/a/5235) HimBromBeere's answer; despite them forgetting the brackets in the example code, the answer did clearly state you need to put the initialization in the class constructor, which was the solution to your problem. – Nyerguds Mar 24 '16 at 09:33
  • @Nyerguds this has been done now, again thank you to all for helping with this. – Graeme Phillips Mar 24 '16 at 09:55

3 Answers3

16

Setting a default-value for auto-implemented properties is only available from C#-version 6 and upwards. Before Version 6 you have to use the constructor and set the default-value there:

public class PlayerCharacter {
    public int Health { get; private set; }
    
    public PlayerCharacter()
    {
        this.Health = 100;
    }
}

To enable the compiler for VS2013 you may use this approach.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • thanks HimBromBeere - but where do I place that in the code I've copied in the main question above? sorry bit of a noob, just learning. go easy, thanks. Graeme – Graeme Phillips Mar 23 '16 at 13:56
  • Directly into the `PlayerCharacter`-class, prefereably at its top. – MakePeaceGreatAgain Mar 23 '16 at 13:58
  • hi HimBromBeere - I think ive done something wrong... here is the new code, which it doesn't like at all, sorry. namespace GameCore { public class PlayerCharacter { public int Health { get; private set; } public PlayerCharacter { this.Health = 100; } public void Hit(int damage) { Health -= damage; if (Health <= 0) { IsDead = true; } } public bool IsDead{ get; private set; } } } – Graeme Phillips Mar 23 '16 at 14:11
  • sorry, but totally lost now.. If I try that, then I get 7 errors instead of 1 error. – Graeme Phillips Mar 23 '16 at 14:26
  • the Health variable only show intellisense within the Hit method. – Graeme Phillips Mar 23 '16 at 14:27
  • `public PlayerCharacter` is your _class constructor_, not a property. It's a function; it needs `()` behind the name. – Nyerguds Mar 23 '16 at 14:29
  • thank you very much Nyerguds... making it a function has worked! 100% – Graeme Phillips Mar 23 '16 at 14:36
  • Was pulling my hair out over this, thanks for the lead. Windows flaked out and swapped from VS 2015 to VS 2013 on me. Your answer gave me the solution, set VS 2015 as default .... – David C Fuchs Jul 01 '20 at 20:46
3

It looks like this error happens due to version your MSBuild, old version of MSBuild can only compile C# version 4, while your code written in C# version 6 format (set default value for properties).

Example of code writing in C# version 6:

 public static string HostName { get; set; } = ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";

For MSBuild to compile your code, you need to write in C# 4 style

public static string HostName { get; set; }
public SomeConstructor()
        {
            HostName = ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";... }

Or

 public static string HostName
        {
            get
            {
                return ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";
            }
        }

Hope it helps

Hung Vu
  • 5,624
  • 2
  • 30
  • 27
  • This comment helped me more than the above, because I could build my project with no issues from my VS2017 on my computer, but I got this build failure on our CI system (TeamCity) afterwards, which was using an older version of MSBuild to build the project. So reverting the C# 6.0 property autoinitialization to an older C# syntax compatible with that MSBuild sorted out that issue - and pointed me towards the need to update our TeamCity infrastructure! – Jesus Campon Feb 08 '19 at 12:41
0

the answer was:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameCore
{
    public class PlayerCharacter 
    {

        public int Health { get; private set; }

        public PlayerCharacter()
        {
            this.Health = 100;
        }


        public void Hit(int damage)
        {
            Health -= damage;


            if (Health <= 0)
            {
                IsDead = true;
            }
        }




        public bool IsDead{ get; private set; }

    }
}

making the constructor a function with () and not as PLayerCharacter{ etc.

thanks to all, back into my hole I go.

Graeme Phillips
  • 131
  • 1
  • 1
  • 10