2013-06-21 55 views
2

我正在創建一個OPC客戶端,從而可以讀取電子設備的標籤。這是通過在OPC組中設置項來完成的。我想要一個屬性爲bool []的數組來將項目設置爲活動或非活動狀態。我需要知道該屬性bool []的哪個索引用於設置屬性,以便我可以使用它來激活該項目。我可以只使用一種方法,但更喜歡一個屬性。 _theGroup是持有這些項目的OPC組。確定使用c設置數組屬性的哪個索引#

private bool[] _ItemActive;  
public bool[] itemActive { 
    get { return _ItemActive; } 
    set { 
     if (_theGroup != null) { 
      int itemIndex = ?? // I don't know how to find property index access by user 
      int itemHandle = itemHandles[itemIndex]; //uses index to discover handle 
      _theGroup.SetActiveState(itemHandle, value, out err); // use handle to set item active 
     } 
     _ItemActive = value; 
    } 
} 

用戶會做這樣的事情。

opcClient.itemActive[3] = false; 

我需要能夠發現3將其插入的ItemIndex我的我的布爾數組的二傳手如上圖所示[]

回答

5

您可以創建自定義集合與被覆蓋的索引操作,在那裏你會做你的自定義邏輯。但創建一個setter方法SetItemActivation(int,bool)似乎是一個更乾淨的解決方案。

+0

我需要能夠在實例化類之前和之後設置活動狀態。第一個_theGroup將爲空,因此它只會設置該屬性。然後,當_theGroup開始時,它使用這個屬性來確定它們是否應該開始活動。然後,在_theGroup運行並且不爲空之後,我必須用該組的SetActiveState方法激活它。所以我試圖讓一個財產去做這兩件事。但我同意一個setter方法會更乾淨... – user2510313

0

我發現了一個解決方案:創建一個新的類,其實例跟蹤對其條目的更改;每當索引值發生變化時,就會觸發一個事件,告訴哪個條目發生了變化,並告知哪個值。這裏有一個這樣(下面的代碼可以使用泛型來代替布爾變量更通用的課程)一類:

// This class tracks changes to its entries 
public class BoolArray_Tracked{ 
    // Set up the data for which changes at specific indices must be tracked 
    private bool[] _trackedBools; 
    public bool this[int itemIndex]{ 
    get { return _trackedBools[itemIndex]; } 
    set { 
     // check if something really changed 
     // not sure if you want this 
     if(_trackedBools[itemIndex] != value){ 
     _trackedBools[itemIndex] = value; 
     OnIndexChanged(itemIndex, value);// Raise an event for changing the data 
     } 
    } 
    } 

    // Constructor 
    public BoolArray_Tracked(bool[] _trackedBools){ 
    this._trackedBools = _trackedBools; 
    } 

    // Set up the event raiser 
    public delegate void IndexEventHandler(object sender, IndexEventArgs a); 
    public event IndexEventHandler RaiseIndexEvent; 

    // Raise an event for changing the data 
    private void OnIndexChanged(int indexNumber, bool boolValue){ 
    if (RaiseIndexEvent != null) 
     RaiseIndexEvent(this, new IndexEventArgs(indexNumber, boolValue)); 
    } 
} 

// This is class enables events with integer and boolean fields 
public class IndexEventArgs : EventArgs{ 
    public int Index{get; private set;} 
    public bool Value{get; private set;} 

    public IndexEventArgs(int _index, bool _value){ 
    Index = _index; 
    Value = _value; 
    } 
} 

下實現OPC類:

class OPC_client{ 
    // Tracked bool array 
    public BoolArray_Tracked ItemActive{get; set;} 

    // Constructor 
    public OPC_client(bool[] boolItemActive){ 
    ItemActive = new BoolArray_Tracked(boolItemActive); 
    ItemActive.RaiseIndexEvent += Handle_ItemActive_IndexChange; 
    } 

    // State what should happen when an item changes 
    private void Handle_ItemActive_IndexChange(object sender, IndexEventArgs e){ 
    //if (_theGroup != null) {//Your code in the question 
     int itemIndex = e.Index; 
     //int itemHandle = itemHandles[itemIndex]; //Your code 
     //_theGroup.SetActiveState(itemHandle, value, out err); // Yours 
    //} //Your code 

    Console.WriteLine("The array `ItemActive' was changed" 
     + " at entry " + itemIndex.ToString() 
     + " to value " + e.Value.ToString()); 
    } 
} 

最後實現一個簡單的程序測試這

static void Main(string[] args){ 
    OPC_client opcTest = new OPC_client(new bool[]{true, false}); 
    Console.WriteLine("Testing if message appears if value stays the same"); 
    opcTest.ItemActive[0] = true; 

    Console.WriteLine(); 
    Console.WriteLine("Testing if message appears if value changes"); 
    opcTest.ItemActive[0] = false; 
    Console.ReadLine(); 
}