2

Ive been researching and I am a little stuck on finding the right answer.

Lets say I have a c# class with auto properties. I want to have some of these properties calculated based of the properties that a user will change. I understand that you can use a constructor to do this calculation on creation of a new object.

What I am trying to find out is, in a web api does the class constructor get called on an update? Do i do the following or create customer setters?

eg

class myclass 
{
    public int Num1 { get; set; }
    public int Num2 { get; set; }
    public int Num3 { get; set; }
    public int Num4 { get; set; }

    //these get changed when values above get changed by API
    public int result1 { get; set; } 
    public int result2 { get; set; }
    public int result3 { get; set; } 

    //constructor
    public myClass()
    {
        result1 = Num1 + Num2;
        result2 = Num3 + Num2;
        result3 = Num4 + Num2;
    {
}
3
  • 1
    probably you can use INotifyPropertyChanged (reference: learn.microsoft.com/en-us/dotnet/api/…) Commented Nov 12, 2018 at 1:46
  • Once the Class is construted, the constructor never runs Again. To set properties afterwards, simply do: Num1 = whatever; Commented Nov 12, 2018 at 1:46
  • 1
    What you considered changing result1 to be a read-only property? Commented Nov 12, 2018 at 2:17

2 Answers 2

1

if I understand correctly, you simply need some derived readonly properties:

class myclass
{
    public myclass()
    {

    }

    public int Num1 { get; set; }
    public int Num2 { get; set; }
    public int Num3 { get; set; }
    public int Num4 { get; set; }


    public int result1 => Num1 + Num2;
    public int result2 => Num3 + Num2;
    public int result3 => Num4 + Num2;

}
Sign up to request clarification or add additional context in comments.

Comments

0

What you need to use is INotifyPropertyChanged, see the minimal example below

void Main()
{
    var test = new myClass();
    test.Num1 = 1;
    test.Num2 = 2;
    test.Num3 = 3;
    test.Num4 = 4;

    test.result1.Should().Be(3);
    test.result2.Should().Be(5);
    test.result3.Should().Be(6);

    test.Num1 = 2;
    test.result1.Should().Be(4);
    test.Num2 = 0;
    test.result2.Should().Be(3);
    test.result3.Should().Be(4);
}

class myClass : INotifyPropertyChanged
{
    // private setter since these values are only being set when the num{i} are updated
    public int result1 { get; private set; }
    public int result2 { get; private set; }
    public int result3 { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;
    public myClass()
    {
        PropertyChanged += new PropertyChangedEventHandler(UpdateResultValue);
    }

    private void UpdateResultValue(object sender, PropertyChangedEventArgs e)
    {
        result1 = num1 + num2;
        result2 = num2 + num3;
        result3 = num2 + num4;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    private int num1;
    public int Num1
    {
        get => num1;
        set => SetField(ref num1, value);
    }

    private int num2;
    public int Num2
    {
        get => num2;
        set => SetField(ref num2, value);
    }

    private int num3;
    public int Num3
    {
        get => num3;
        set => SetField(ref num3, value);
    }

    private int num4;
    public int Num4
    {
        get => num4;
        set => SetField(ref num4, value);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.