1
public ICommand ChangeLangCommand => new DelegateCommand(this.ChangeLangClick);

I get this compiler error, marked on the => arrow:

Error   1   ; expected

Am I using the wrong compiler version somehow? How can I check this?

Tor Klingberg
  • 4,790
  • 6
  • 41
  • 51
  • 2
    I don't think it is caused by this line, have you checked the lines above make sure they're all terminated? – Callum Linington Aug 30 '16 at 11:09
  • Right-click on your project (not the solution), and choose properties. From there you can see what version of .NET you're compiling and can change it. – Drew Kennedy Aug 30 '16 at 11:11
  • What version of c# are you using? – Nkosi Aug 30 '16 at 11:12
  • 1
    I think that even with the new compiler a read-only property `public ICommand ChangeLangCommand {get;} = new DelegateCommand(this.ChangeLangClick);` would be better than a computed property that you are trying to use. – Sergey Kalinichenko Aug 30 '16 at 11:14
  • It says ".NET Framework 4 Client Profile". Is that what you meant. I am using VS2013. Will I need to update all of Visual Studio? – Tor Klingberg Aug 30 '16 at 11:14
  • 1
    Maybe you want to assign the delegate command instead of doing some half-syntax lambda? `public ICommand ChangeLangCommand = new DelegateCommand(this.ChangeLangClick);` – grek40 Aug 30 '16 at 11:14
  • The code is from this file found on GitHub, so I am not really interesting in refactoring the code, just getting it to compile: https://github.com/snmslavk/WPF-Keyboard-Control/blob/master/TermControls/ViewModels/KeyboardViewModel.cs – Tor Klingberg Aug 30 '16 at 11:15
  • 4
    @TorKlingberg, That code if for C#6.0 which you can only use in more recent versions of VS. The syntax you showed wont work on VS2013. You can either upgrade your version of VS or refactor your code – Nkosi Aug 30 '16 at 11:16
  • 2
    Get VS 2015 (update 3 or 4 is latest) – Sarvesh Mishra Aug 30 '16 at 11:18
  • Have a read of http://stackoverflow.com/questions/14180688/difference-between-delegatecommand-relaycommand-and-routedcommand – Paul Zahra Aug 30 '16 at 11:19

1 Answers1

1

This is a C# 6.0 feature called expression bodied property

public ICommand ChangeLangCommand => new DelegateCommand(this.ChangeLangClick);

You can either upgrade your compiler (install latest release version of VS2015) or don't use it, as it's equal to getter-only property:

public ICommand ChangeLangCommand
{
    get
    {
        return new DelegateCommand(this.ChangeLangClick);
    }
}

I have feeling, what creating new instance of the command every time property is accessed is wrong, correct code may/would be

public ICommand ChangeLangCommand { get; }

// in constructor
ChangeLangCommand = new DelegateCommand(this.ChangeLangClick);

I think this is also a new feature (to initialize getter-only property from constructor), if this is true, then you can use older syntax:

ICommand _changeLangCommand;
public ICommand ChangeLangCommand
{
    get
    {
        return _changeLangCommand;
    }
}

// in constructor
_changeLangCommand = new DelegateCommand(this.ChangeLangClick);
Sinatr
  • 20,892
  • 15
  • 90
  • 319