2011-02-24 109 views
0

我有10個屬性,當每個設置它增加變量的值Value。當Value的值爲10時,該應用將結束。但它似乎是尷尬寫在相同的條件爲他們每個人是這樣的:具有遞增一個變量的多個屬性。簡單檢查其價值?

int Value=0; 
int A 
{ 
    set 
    { 
    a=value; 
    Value++; 
    if(Value>10) ... //check here 
    } 
} 
int B 
{ 
    set 
    { 
    b=value; 
    Value++; 
    if(Value>10) //check here again 
    } 
} 

我怎麼能簡化檢查它的價值?

回答

0
private int counter; 
private int b; 
private int a; 

public int A 
{ 
    set 
    { 
     counter++; 
     a = value; 
     CheckCounter(); 
    } 
} 

public int B { 
    set 
    { 
     counter++; 
     b = value; 
     CheckCounter(); 
    } 
} 


public void CheckCounter() 
{ 
    if (counter>10) 
    { 
     //Do something 
    } 
} 

或使計數器屬性..

private int Counter 
{ 
    set 
    { 
     counter = value; 
     CheckCounter(); 
    } 
    get 
    { 
     return counter; 
    } 
} 

和增加時使用此..

Counter++; 
1

您可以爲Value創建一個私有屬性,並在該屬性的設置器中,如果該值設置爲10以上,則退出應用程序。

private int value=0; 
private int a, b; 

public int A 
{ 
    set 
    { 
     this.a = value; 
     Value++; 
    } 
    get { return this.a; } 
} 
public int B 
{ 
    set 
    { 
     this.b = value; 
     Value++; 
    } 
    get { return this.b; } 
} 
private int Value 
{ 
    set 
    { 
     this.value = value; 
     if (this.value > 10) 
     { 
     // Exit. 
     } 
    } 
    get { return this.value; } 
} 
+0

值將需要一個gettter增加... – 2011-02-24 09:19:00

+0

我知道,我只是試圖保持例子簡短。我已經更新了我的答案,現在包括getter。 – 2011-02-24 09:36:51

0

從@Richard和@Emil這兩個答案都是正確的,但爲了使您的代碼在將來更具可擴展性,最好實施內置的INotifyPropertyChanged interface

class Data : INotifyPropertyChanged 
{ 
    private int _counter = 0; 

    public event PropertyChangedEventHandler PropertyChanged = (sender, arg) => 
     { 
      // if (_counter > 10) Exit(); 
      // from arg you can know what property is changed 
      // which is probably used for future 
     }; 

    private int _number; 
    public int Number 
    { 
     get { return _number; } 
     set 
     { 
      //here is another unclear point in your question 
      //will the counter increases when setting a.Number = 100 
      //but it's already 100 before setting 
      if (_number != value) 
      { 
       _number = value; 
       PropertyChanged(this, new PropertyChangedEventArgs("Number")); 
      } 
     } 
    } 
} 
1

我假設你的屬性是它具有其他責任比退出程序時,櫃檯命中10.如果是這樣,我會刪除檢查櫃檯,從離開應用程序的邏輯類類。 也許你可以使用一個事件。這個事件的訂閱者會在Value達到10時通知您(您實際上可以設置「10」,但這只是一個簡短的例子)並退出應用程序。

class YourClass { 
    public event ValueHandler ValueIs10; 
    public EventArgs e = null; 
    public delegate void ValueHandler(YourClass m, EventArgs e); 

    private int _value=0; 

    public int Value { 
     get {return _value;} 
     private set { 
      _value=value; 
      if(_value==10 && ValueIs10 != null) ValueIs10(this, e); 
     } 
    } 

    public int A 
    { 
     set 
     { 
     a=value; 
     Value++; 
     } 
    } 

    public int B 
    { 
     set 
     { 
     b=value; 
     Value++; 
     } 
    } 

    // ... 
} 
+0

在我看來,這是最好的答案。雖然其他答案的工作,這一個有最好的設計。 – 2011-02-24 09:48:10