0

Problem: Cannot set backing field of property to private as I get the following exception when setting a value to Name.

System.ArgumentException : You must declare a 
backing field for this property named: _Name

My Code:

public class MyVM : ReactiveObject
{
    private string _Name;
    public string Name
    {
        get { return _Name; }

        set { this.RaiseAndSetIfChanged(x => x.Name, value); }
    }
}

To Fix this i have been able to set _Name to public:

    public string _Name;

Which fixed the problem, but why do I have to expose the backing field as public? The examples I see on the Net use private backing fields...?

Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118

1 Answers1

2

Use the new overload instead, unless you're not using >= VS2012:

this.RaiseAndSetIfChanged(ref theBackingField, value);
Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • Thanks!!! Worked perfectly! Great job on System.Reactive and ReactiveUI!!!Tried googling for this but couldn't find it anywhere! :D – Michal Ciechan Sep 22 '13 at 21:41