如果一個類只包含字符串數組變量,是否有簡單的方法來初始化它們而不必反覆輸入相同的代碼?在類中簡單初始化字符串數組
舉例來說,如果我有像
[Serializable]
public class ReadDiagnosticEntirePointValuesResponse
{
public string[] pointidentifier;
public string[] presentvalue;
public string[] priorityarray;
public string[] alarmstate;
public string[] outofservice;
public string[] correctvalue;
public string[] affect;
public string[] covenable;
public string[] covincrement;
public string[] covtarget;
public string[] covlifetime;
public string[] historyenable;
public string[] historyincrement;
public string[] elapsedactivetime;
public string[] feedbackvalue;
public string[] rawvalue;
...//A lot more
}
,我想值分配給他們,我想避免這樣做:
ReadDiagnosticEntirePointValuesResponse response = new ReadDiagnosticEntirePointValuesResponse();
response.affect = new string[count];
response.alarmstate = new string[count];
response.correctvalue = new string[count];
response.covenable = new string[count];
response.covincrement = new string[count];
response.covlifetime = new string[count];
response.covtarget = new string[count];
response.elapsedactivetime = new string[count];
response.feedbackvalue = new string[count];
response.historyenable = new string[count];
response.historyincrement = new string[count];
response.outofservice = new string[count];
response.pointidentifier = new string[count];
response.presentvalue = new string[count];
response.priorityarray = new string[count];
response.rawvalue = new string[count];
...
當然,我可以寫在這些初始化構造函數,但仍然不能讓我不必手動初始化它們。
什麼是避免這種情況的好方法?
我建議你重新考慮你的設計。在這種情況下,它看起來像只需要*一個*結構化數據數組。 –
爲什麼不能使用'List'或'List ',其中'CustomClass'包含所有數據 –
Raghuveer
同意@Konrad; YourTypeHere []'YourTypeHere'定義了一個'PointIdentifier','PresentValue','AlarmState'等等。也;我懷疑他們都需要成爲字符串! –