2012-09-13 24 views
1

如果一個類只包含字符串數組變量,是否有簡單的方法來初始化它們而不必反覆輸入相同的代碼?在類中簡單初始化字符串數組

舉例來說,如果我有像

[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]; 
     ... 

當然,我可以寫在這些初始化構造函數,但仍然不能讓我不必手動初始化它們。

什麼是避免這種情況的好方法?

+0

我建議你重新考慮你的設計。在這種情況下,它看起來像只需要*一個*結構化數據數組。 –

+0

爲什麼不能使用'List '或'List ',其中'CustomClass'包含所有數據 – Raghuveer

+0

同意@Konrad; YourTypeHere []'YourTypeHere'定義了一個'PointIdentifier','PresentValue','AlarmState'等等。也;我懷疑他們都需要成爲字符串! –

回答

3

這是一個非常可怕的方式來管理你的數據,但是,像下面這樣將工作 ....

foreach(var field in GetType().GetFields()) { 
    if(!field.IsStatic) field.SetValue(this, new string[count]); 
} 

但是!我強烈建議你重新考慮這個設計。一個更好的機制將是:

class DiagnosticPoint // TODO: rename as appropriate 
{ // TODO: check these all need to be strings 
    public string Affect {get;set;} 
    public string AlarmState {get;set;} 
    ... 
    public string RawValue {get;set;} 
} 

和具有該陣列作爲字段:

public class ReadDiagnosticEntirePointValuesResponse 
{ 
    DiagnosticPoint[] points; 
    ... 

然後簡單地初始化數組:

points = new DiagnosticPoint[count]; 

和INIT各:

for(int i = 0 ; i < count ; i++) points[i] = new DiagnosticPoint(); 

and access via:

var alarm = points[index].AlarmState; 

(等)

0

您可以使用數組列表來避免聲明大小。

+0

但是有固定長度的數組可能有一個很好的理由。可能需要精確的「數量」元素,不多也不少。 – carlpett

+0

數組列表???別 – Habib

0

至少一旦你必須手動初始化它們。如果C'tor不適合你,請用新方法初始化。

0

你可能想看看該反思......閱讀類的性質研究在一個循環

1

您可以使用Reflection做到這一點:

public ReadDiagnosticEntirePointValuesResponse() 
{ 
    GetType().GetFields(BindingFlags.Instance | BindingFlags.Public) 
      .ToList() 
      .ForEach(field => field.SetValue(this, new string[count])); 
} 
0

這聽起來像一個可怕的類定義。也許使用enum可能會更好。

如果你可以用這個啓動:

public enum ReadDiagnostic 
{ 
    pointidentifier, 
    presentvalue, 
    priorityarray, 
    alarmstate, 
    outofservice, 
    correctvalue, 
    affect, 
    covenable, 
    covincrement, 
    covtarget, 
    covlifetime, 
    historyenable, 
    historyincrement, 
    elapsedactivetime, 
    feedbackvalue, 
    rawvalue, 
} 

然後,您可以創建一個Dictionary<ReadDiagnostic, string[]>來保存你的價值觀,你是存儲類。

你甚至可以使用LINQ來創建你的字典中的一行代碼:

var readDiagnosticEntirePointValues = 
    typeof(ReadDiagnostic) 
    .GetEnumValues() 
    .Cast<ReadDiagnostic>() 
    .ToDictionary(x => x, x => new string[count]); 

這種方法仍然是強類型,但更容易維護比你目前的做法。

0

您可以使用下列內容:

response.affect = response.alarmstate = response... = new string[count]; 

但是,使用此仍會有手動初始化。這只是一條捷徑。

正如其他人可能已經建議的那樣,使用Collection Classes將使它更容易。我會推薦一個Dictionary。這裏是你如何可以實現它:

enum Foo { Affect, AlarmState, CorrectValue, ... } 

public void InitializeArrays(int count) 
{ 
    Dictionary<Foo, string[]> response = new Dictionary<Foo, string[]>(); 

    // easy initialization of string arrays 
    foreach (Foo foo in Enum.GetValues(typeof(Foo))) 
    { 
     response.Add(foo, new string[count]); 
    } 

    // use this to access the string arrays 
    response[Foo.Affect][0] = "test"; 

    if (response[Foo.CorrectValue].Length > 0) { ... } 
} 

或者,您也可以通過使用Multidimensional Array達到相同的。

// initialize it like this 
string[,] response = new string[Enum.GetValues(typeof(Foo)).Length, count]; 

// access it like this 
response[(int)Foo.Affect, 0] = "test";