2012-08-27 39 views
1

我正在嘗試構建一個數據結構,可以通過一組不同測試的跟蹤結果存儲試用版。測試全部由多條路徑組成,但我想要保存和稍後使用的一些信息對於不同的測試是不同的。例如,種皮的結果可能是:如何將任意數量的任意類型的數組存儲在字典中

Trial(int) WasResponseCorrect(bool) WhichButtonWasPressed(string) SimulusLevel(double) 

    1     false       "Up"       3.5 
    2     true       "Left"      6.5 

凡TESTB可能有不同類型的結果字段:

Trial(int) WasResponseCorrect(bool) ColorPresented(string) LetterPresented(char)  LetterGuessed(Char) 
    1      false      green      G      C 

    2      false      blue      H      F 

我想創建一個字典,字段名作爲鍵(例如WasResponseCorrect)和一個字段值數組作爲dic的值。我無法弄清楚如何做到這一點。也許有更好的方式來存儲信息,但我想不出如何去做。我正在使用.net(VB和C#),但我認爲如果您知道其他語言的示例,我可以理解並轉換大多數任何代碼。謝謝!

+1

只需將信息存儲在'DataTable'中即可。它被設計用於存儲任意數量的數據行,每個數據行具有任何數量的列,每個列在編譯時都是未知的。 – Servy

+0

我錯過了什麼或'Dictionaty '是明顯的答案? – devundef

+0

這正是我正在尋找的!謝謝。如果您發佈答案,我可以接受。 –

回答

4

不知道更多關於您的需求(例如您將如何存儲數據),似乎多晶現象就是您要查找的內容。也就是說,您有一個超類(稱爲Trial)和代表特定試用類型的子類。例如:

public class Trial { 
    public int Id { get; set; } 
    public bool WasResponseCorrect { get; set; } // if this is in every type of trial 
    // anything else that is common to ALL trial types 
} 

public class TrialA : Trial { 
    public string WhichButtonWasPressed { get; set; } 
    public double SimulusLevel { get; set; } 
} 

public class TrialB : Trial { 
    public string ColorPresented { get; set; } 
    public char LetterPresented { get; set; } 
    public char LetterGuessed { get; set; } 
} 

這樣,你可以有Trial對象的列表,但這些對象的實際運行時類型可以是TrialATrialB

+1

值得一提的是,這隻適用於在編譯程序時知道每種試驗類型以及這些字段的所有字段和類型。 – Servy

+0

非常真實,Servy。 –

相關問題