0

I have a class consisting of variable members and a function member. The variable member occasionally changes. I want the function to be called automatically upon the variable changes. In other words, how can I tie the variables inside a class?

class line
{
   double x, y; // The poition of the lind end. The line starts at the origin (0,0)
   double l; // The length of the line
   void length()
   {
      l = Math.sqrt(x*x+y*y);
   }
}

In example above, I need the length to be updated when x and y change.

  • How would you envision this mechanism working? This isn't magic...something has to be checking the values of x and y and calling your method once they change... – rory.ap Mar 29 '17 at 17:34
  • 4
    Possible duplicate of [How to trigger event when a variable's value is changed?](http://stackoverflow.com/questions/5842339/how-to-trigger-event-when-a-variables-value-is-changed) – BetterLateThanNever Mar 29 '17 at 17:35

5 Answers5

3

Make your variables into properties, then put your functions in the set accesors.

class line
{
    double _x, _y;

    double x
    {
        get { return _x; }
        set
        {
            _x = value;
            length();
        }
    }

    double y
    {
        get { return _y; }
        set
        {
            _y = value;
            length();
        }
    }

    double l; // The length of the line

    void length()
    {
        l = Math.Sqrt(_x * _x + _y * _y);
    }
}
Ben J
  • 1,040
  • 1
  • 5
  • 13
  • Thanks, Ben. Now I have another problem. I wrapped x and y over a class. Therefore x and y are updated without setting. Since I never set the variables, length() never be called. – Hossein Dehghani Mar 29 '17 at 18:13
  • You may want to look at events and event handlers – Ben J Mar 30 '17 at 12:12
2

If you define properties, on your class, you can make X and Y autoprops, then make a read-only property L that is calculated from these values:

public class Line //class names should be Capitalized
{
   public double X{ get; set; } //prop names should be Capitalized
   public double Y{ get; set; }
   public double L{
    get{
      return Math.Sqrt(X * X + Y * Y);
    }
   }
}
spender
  • 106,080
  • 28
  • 202
  • 324
1

you can you properties

int x
int X {
   get { return x; }
   set { x = value; YouMethod();}
}
Alex S
  • 111
  • 6
1
  1. You can achieve pretty similar behavior using calculated property like

    double Length
    {
        get { return Math.sqrt(x*x+y*y); }
    }
    

    The only caveat is that calculation is performed upon each call to Length even if x and y haven't changed.

  2. You can encapsulate you x and y fields into properties and call length function from setter like

    double X
    {
        get { return x; }
        set 
        {
            x = value;
            length();
        }
    }
    
    double Y
    {
        get { return y; }
        set 
        {
            y = value;
            length();
        }
    }
    

    and then change x and y ONLY via X and Y properties.

0

As BenJ noted, you can use properties.

Instead of declaring x and y as simple fields inside the class. You can declare them as properties the following way :

private double x;
public double X
get
    {
        return this.x;
    }

    set
    {
        this.x = value;

        this.length()
        //Above line will call your desired method
    }
Kobek
  • 905
  • 4
  • 13
  • 32