开发者

c# question about properties from base class

It seems like I should know this and I thought I did. I have base view model class, in it I have several properties but they all make use of automatic getters and setters so of course those all work fine, but I have one that has to broadcast an event (I am using prism) so it looks like this.

protected bool isValid;
public bool IsValid
{
  get { return isValid; }
  set
  {
    isValid = true;
    this.EventAggregator.GetEvent<ViewModelValidEvent>().Publish(isValid);
  }
}

The problem I get is when I try to assign IsValid in a class that is derived from it - the compiler error says that it is read only... I thought protected was what I need to do to fix it did not.

Furthermore - I keep having to declare these private v开发者_高级运维ariables for everything when I need the setter to call RaisePropertyChanged or whatever... Is there a better way to do this?

Can anyone point me in the right direction? Thanks!!!


I suspect you're trying to use isValid on an object which isn't known (by the compiler) to be an instance of the derived class or a subclass of the derived class. protected has relatively subtle semantics like that.

Personally I would encourage you to keep your fields private though, and use properties from any other class - even a derived class.

As for a simpler way of implementing the properties - I don't think there is one really, no. You could have a wrapper type, but you'd still need to create a variable to hold the instance of the wrapper type, and the get/set accessors to proxy to the variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜