2013-05-06 2 views
0

업데이트 : 나는 틀린 질문을 표현했습니다. 내부 및 외부 위도/경도 멤버 모두 서로를 업데이트해야 재귀가 발생합니다. 외부 멤버는 데이터베이스 컨텍스트에 의해 업데이트되고 내부 멤버는 UI에 의해 업데이트됩니다. 따라서 데이터베이스의 모든 변경 사항을 UI에 전파해야하며 반대의 경우도 마찬가지입니다. Coordinates 클래스는 UI와 분리 할 수 ​​없으며 외부 멤버를 데이터베이스에서 분리 할 수 ​​없습니다.콜백 함수 및 모범 사례

재귀 방지 플래그는이 문제를 해결하는 가장 간단한 방법이라고 생각합니다.

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. */ } 
    } 
} 

회원 CoordinateLatitude과 좌표 클래스에 CoordinateLongitude지도 :

는 나는 다음과 같은 클래스가 있습니다. 내가 성취하고자하는 것은 그 반대입니다. 따라서 Coordinates 클래스가 업데이트되면 Location 클래스의 멤버도 업데이트해야합니다. 지금 코드를 변경하는 자유가 없기 때문에 코드가 왜 구조화되어 있는지 묻지 마십시오.

등의 클래스를 구성하는 코드가 프로젝트 전체에 걸쳐 있습니다. Coordinates 클래스 (및 기타 많은 클래스)는 가벼운 데이터 구조이므로 많은 곳에서 부주의하게 사용되므로 이벤트에 넥타이하고 싶지는 않습니다.

내 질문은 어떻게 Location.Coordinates 멤버가 Location.CoordinateLatitude 값을 업데이트하도록 할 수 있습니까? IDisposable (이벤트가있는 경우와 같이)을 정리하지 않고 이러한 콜백을 설정하는 표준 방법이 있습니까? LongitudeCoordinateLongitude에서

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. */ } 
    } 

변경 Latitude :

+0

이벤트를 원한다고해도 속성 이벤트 리스너가 필요합니다. –

+1

이것은 이미 사용자가 묻는 것을 수행하는 것으로 보입니다 ... – FlyingStreudel

+2

'set' 메소드를 연결하는 방법은 기본 설정과 같습니다. 'get' 메소드를 사용하면 원하는 것을 할 수 있습니다. 콜백이 필요 없습니다. public 속성 인 'CoordinateLatitude'와 'CoordinateLongitude'를 'Coordinates'멤버의 별칭으로 생각하십시오. –

답변

1

그러나 CoordinateLatitude는
내가 UI이다 전혀 분명하지 않다

를 업데이트하지 않습니다 왜 당신이 물어 무슨 뜻인지

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()); 

아마도 코드를 테스트 업데이트됩니까 내부와 어느 outters이며 어디 rerecursion지고 있습니다.
그들은 서로를 업데이트하지 않습니다.
두 개체는 모두 동일한 개체를 참조합니다.

나를 위해 작동합니다. Coordinates 이후

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; } 
    } 
} 
0

코드에서 버그가 있습니다. 또한 별도로 보관하는 것은 권장하지 않습니다. 좌표는 좌표에 대해 위도와 경도를 모두 요구하는 점에 일반적으로 해당되므로 독립적으로 저장하는 것은 의미가 없습니다. 이 경우 한 번만 전화하면 CoordinateLatitude/Longitude을 모두 제거 할 수 있습니다. 말했다되고 그건

은, 당신이 할 일은 이러한 세터 추가 할 수 있습니다 : 당신이 그것을 변경, 그래서 만약 어떤 변수를

set { this.Coordinates.Latitude = value; }

0

Coordinates

set { this.Coordinates.Longitude = value; }

이 클래스를 그 동일한 인스턴스가 자동으로 업데이트됩니다. 나는 당신의 세트 그냥 할 수있는 제대로 수행하려는 이해하고있어 가정 :

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; } 
} 

편집 :이 Coordinates는 구조체 인 경우에 다르게 할 필요가있다. 이 경우 this.Coordinates이 복사본을 회수하기 때문입니다.

public double CoordinateLongitude 
{ 
    get { return (this.Coordinates.Longitude); } 
    set { 
     Coordinate temp = this.Coordinates; 
     temp.Longitude = value; 
     this.Coordinates = temp; 
    } 
} 
0

이미 CoordinateLatitude에서에서 당신의 독서, 당신은 이미 수행 한 Location의 속성입니다. Location.Coordinate.Latitude에서 읽기는 Location.CoordinateLatitude에서 읽기와 같습니다. 당신의 세터에

public Coordinates Coordinates { get; set; } 

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

당신은 Location.Coordinate.Latitude 값을 설정하거나 그냥 읽기 전용 속성을 확인하기 위해 설정을 삭제하기를 업데이트 할 수 있습니다.