-1

My view model is bound to an object from a WCF service. When using the ObservableObject.Set<>. I get the following error:

// Error: A property or indexer may not be passed as an out or ref parameter.
public string SomeProperty
{
    get { return _wcfObject.SomeProperty; }
    set { Set(nameof(SomeProperty), ref _wcfObject.SomeProperty, value); }
}

Now obviously I can't do this, and these attempted workarounds don't work.

// Error: } expected
public string SomeProperty
{
    get { return _wcfObject.SomeProperty; }
    set { 
            ref string v = _wcfObject.SomeProperty;
            Set(nameof(SomeProperty), ref v, value); 
        }
}

// Compiles, but property not updated.
public string SomeProperty
{
    get { return _wcfObject.SomeProperty; }
    set { 
            var v = _wcfObject.SomeProperty;
            Set(nameof(SomeProperty), ref v, value); 
        }
}

How do I make this work with MVVMLight without having to wrap the object from the WCF service?

Jake
  • 1,701
  • 3
  • 23
  • 44

1 Answers1

1

Of course it won't work, because you are updating the local variable v and discarding it after the setter is executed.

You either have to check manually and then rise the property changed event yourself, or you assign v to _wcfObject.SomeProperty afterwards.

public string SomeProperty
{
    get { return _wcfObject.SomeProperty; }
    set { 
            var v = _wcfObject.SomeProperty;
            Set(nameof(SomeProperty), ref v, value);
            _wcfObject.SomeProperty = v;
        }
}

That just looks pretty... "odd". Best may be to use proper backing fields, it's bad practice anyways to directly operate on models when wrapping them around, as you can't discard the changes.

private string someField;
public string SomeProperty
{
    get { return someField; }
    set { 
        Set(nameof(SomeProperty), ref someField, value);
    }
}

public ICommand DoSomethingCommand 
{
    return new DelegateCommand(DoSomething);
}

private void DoSomething()
{
    // apply your ViewModel state to the _wcfObject and do something with it
}

Then your _wcfObject is unaffected by the changes user do, until he initiates an action/command.

Tseng
  • 61,549
  • 15
  • 193
  • 205