Recall function and best practices

UPDATE:I phrased the question wrong. Both the inner and outer Latitude/Longitude members need to update each other resulting in recursion. The outer members get updated by a database context while the inner ones get updated by the UI. So any changes from the database have to be propagated to the UI and vice versa. The Coordinates class CANNOT be separated from the UI and the outer members cannot be separated from the database.

I guess a flag to prevent recursion would be the simplest way around this.

I have the the following classes:

class Coordinates
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

class Location
{
    public string Name { get; set; }

    public Coordinates Coordinates { get; set; }

    public double CoordinateLatitude
    {
        get { return (this.Coordinates.Latitude); }
        set { /* This member needs to get updated every time Location.Coordinates.Latitude gets updated. */ }
    }

    public double CoordinateLongitude
    {
        get { return (this.Coordinates.Latitude); }
        set { /* This member needs to get updated every time Location.Coordinates.Latitude gets updated. */ }
    }
}

The members CoordinateLatitude
and CoordinateLongitude
map to the Coordinates class. What I want to achieve is the reverse as well. So when the Coordinates
class gets updated, the members of the Location
class should also be updated. Don’t ask me why the code is structured as such because I am not at liberty to change it at the moment.

There is a LOT of code throughout the project that sets the members of the Coordinates class: location.Coordinates.Latitude = 10.0D;
, etc. Since the Coordinates
class (and many others) are lightweight data structures, and are used carelessly in many places, I do not want to tie them up to events.

My question is, how can I have the Location.Coordinates member update the Location.CoordinateLatitude values? Is there a standard way to set up such a callback without requiring IDisposable
to cleanup (as in the case with events)?

But CoordinateLatitude does get updated

I tested your code

Location myLoc = new Location();
myLoc.Coordinates = new Coordinates();
myLoc.Coordinates.Latitude = 10;
System.Diagnostics.Debug.WriteLine(myLoc.CoordinateLatitude.ToString());
myLoc.Coordinates.Latitude = 20;
System.Diagnostics.Debug.WriteLine(myLoc.CoordinateLatitude.ToString());

Possibly what you mean to ask is why the UI does not update

It is not at all clear which is inner and which is outter and where you are getting rerecursion.

They don’t (well should’t) update each other.

They both just reference the same object.

This works for me.

class Coordinates
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

class Location : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    public string Name { get; set; }
    private Coordinates coordinates = new Coordinates();
    public Coordinates Coordinates
    {
        get { return coordinates; }
        set
        {
            if (coordinates == value) return;
            coordinates = value;
            NotifyPropertyChanged("Coordinates");
            NotifyPropertyChanged("CoordinateLatitude");
            NotifyPropertyChanged("CoordinateLongitude");
        }
    }

    public double CoordinateLatitude
    {
        get { return (this.Coordinates.Latitude); }
        set { this.Coordinates.Latitude = value; }
    }

    public double CoordinateLongitude
    {
        get { return (this.Coordinates.Longitude); }
        set { this.Coordinates.Longitude = value; }
    }
}
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » Recall function and best practices

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录