2017-02-16 4 views
0
public class Activity 
{ 
    public games _Games {get;set;} 
    public sports _Sports {get;set;} 
} 

public class games : PropertyChangedBase 
{ 
    public int player 
    { 
     get; 
     set; //have if- else statement 
    } 
} 

public class sports : PropertyChangedBase 
{ 
    public int sub{get;set;} 
} 

목표 : 게임 플레이어가 2 이상있는 경우, 나는 스포츠를 하위 변수를 업데이 트하려는 10액세스 상위 레벨 오브젝트

질문 : 부모 클래스에 액세스 할 수있는 방법을 및 스포츠 클래스 변수를 업데이트 하시겠습니까?

+2

이것은 많은 다른 방법으로 잠재적으로 해결 될 수있는 사소한 문제입니다. 시도한 내용과 작동하지 않은 이유에 대한 코드를 표시해주십시오. – Ben

+0

StackOverflow에 오신 것을 환영합니다. 답변이 도움이 되었다면 [답변 수락 방법] (http://meta.stackexchange.com/questions/5234/how-does-accepting)을 살펴보십시오. - 대답 - 직장) 게시물 –

답변

0

Activity 클래스에 업데이트 할 시간임을 알리는 이벤트를 사용할 수 있습니다.

public class games 
{ 
    public event UpdatePlayerSubDelegate UpdatePlayerSub; 

    public delegate void UpdatePlayerSubDelegate(); 
    private int _player; 

    public int player 
    { 
     get { return _player; } 
     set 
     { 
      _player = value; 
      if (_player > 2) 
      { 
       // Fire the Event that it is time to update 
       UpdatePlayerSub(); 
      }     
     } 
    }   
} 

활동 클래스에서 이벤트를 생성자에 등록하고 필요한 업데이트를 이벤트 처리기에 쓸 수 있습니다. 10 귀하의 경우 하위에서 :

public class Activity 
{ 
    public games _Games { get; set; } 
    public sports _Sports { get; set; } 

    public Activity() 
    { 
     this._Games = new games(); 
     this._Games.UpdatePlayerSub += _Games_UpdatePlayerSub; 
     this._Sports = new sports(); 
    } 

    private void _Games_UpdatePlayerSub() 
    { 
     if (_Sports != null) 
     { 
      _Sports.sub = 10; 
     } 
    } 
} 

편집 난 그냥 태그 INotifyPropertyChanged을 보았다. 물론이 인터페이스와 제공된 이벤트를 사용할 수도 있습니다. 다시 생성자에서 이벤트에

public class games : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private int _player; 
    public int player 
    { 
     get { return _player; } 
     set 
     { 
      _player = value; 
      if (_player > 2) 
      { 
       // Fire the Event that it is time to update 
       PropertyChanged(this, new PropertyChangedEventArgs("player")); 
      }     
     } 
    }   
} 

그리고 Activity 클래스 레지스터 : 다음과 같은 인터페이스를 구현

public Activity() 
{ 
    this._Games = new games(); 
    this._Games.PropertyChanged += _Games_PropertyChanged; 
    this._Sports = new sports(); 
} 

를 이벤트 핸들러의 몸 선언

private void _Games_PropertyChanged(object sender, PropertyChangedEventArgs e) 
{ 
    if (_Sports != null) 
    { 
     _Sports.sub = 10; 
    } 
} 

그리고 _Sports.sub은 자동으로 업데이트됩니다. 희망이 도움이됩니다. 물론이 업데이트를 수행하는 다른 방법이 있습니다. 그것은 내 마음에 처음 온 것입니다.

0

Activity 인스턴스를 만들어야합니다. 당신은 또한

Activity activity = new Activity(); 
activity._Sports = new sports(); 
activity._Sports.sub = 10; 

또는 그런데 객체 tantalizer

Activity activity = new Activity 
{ 
    _Sports = new sports() 
}; 

activity._Sports.sub = 10; 

을 사용하여 _Sports를 초기화해야, Activitysports의 부모 클래스가 아닙니다. Activity은 회원으로 sports 객체를 보유하고 있습니다. 귀하의 예에서 PropertyChangedBase은 부모 클래스 인 games입니다.