-3

My code has very many of these constructs:

    ParamViewModel[] _btns;
    public ParamViewModel[] Btns
    {
        get => _btns;
        set => SetProperty(ref _btns, value);
    }

Is there any way to do this with a generic to enable me to cut down of the lines of code as they are all the same but with just a different data type, string, int, Color etc.

For reference here's what SetProperty does:

public class ObservableObject : INotifyPropertyChanged
{

    protected virtual bool SetProperty<T>(
        ref T backingStore, T value,
        [CallerMemberName]string propertyName = "",
        Action onChanged = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
            return false;

        backingStore = value;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

}
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 2
    What does `SetProperty` do? Can you provide its implementation? If all it does is set the field, you can probably use an [autoproperty](https://stackoverflow.com/questions/6001917/what-are-automatic-properties-in-c-sharp-and-what-is-their-purpose) – John Wu Oct 08 '18 at 23:13
  • 1
    the get is redundant; internaly when you define properties the code creates a _btns for you (you just don't see it). Regarding of the set, it depends on what SetProperty does, mostly, but let say it does not do anything but just _btns = value, then regular autoproperties; should work – Gonzalo.- Oct 08 '18 at 23:14
  • @Gonzalo.- I didn't know the get is redundant. I've seen hundreds of examples and they I think all used get => – Alan2 Oct 08 '18 at 23:16
  • 1
    check autoproperty, that should work for you – Gonzalo.- Oct 08 '18 at 23:18
  • @JohnWu - I included the code for SetProperty to my question. I don't know about autoproperty and will look into that. – Alan2 Oct 08 '18 at 23:18
  • It was obviously an `INotifyPropertyChanged` question... Auto properties don't work for what you want – Camilo Terevinto Oct 08 '18 at 23:18
  • 1
    it wasn't that obvious becase SetProperty definition was included 1 minute ago – Gonzalo.- Oct 08 '18 at 23:19
  • @Gonzalo.- I think I have seen at least 10 posts about it where it was suggested to use exactly `SetProperty(ref _btns, value)` :D – Camilo Terevinto Oct 08 '18 at 23:20
  • 1
    To answer the actual question: no, that's the shortest possible right now – Camilo Terevinto Oct 08 '18 at 23:26

1 Answers1

0

the simple, idiomatic get and set is this

ParamViewModel[] Btns {get;set;}

You cant get much tighter than that

pm100
  • 48,078
  • 23
  • 82
  • 145
  • I think this might not do all I need because of the SetProperty. I just now added that to the question. @Gonzalo mentioned I might not need a get => . Do you agree on that? – Alan2 Oct 08 '18 at 23:19
  • @CamiloTerevinto - Not sure what you mean when you say "does not compile" as it's used a couple of hundred times in my application. – Alan2 Oct 08 '18 at 23:20
  • @JonathonChase - Thanks for the clarification. Sorry I was mistaken by the comment. – Alan2 Oct 08 '18 at 23:22
  • i typed this before it became clear what set property was doing – pm100 Oct 08 '18 at 23:22