2014-11-20 69 views
0

結構的名字,我想用一個字符串創建一個動態結構的名字,這是我的結構:動態創建在vb.net

Structure foo 
    dim lorem as string 
    dim ipsum as string 
    dim dolor as integer 
    dim sit as integer 
End structure 

在創作

dim bar as new foo 'Bar is temporary, it holds the information and then change its name [I know that's not working like that, imagine] (See below) 
bar.lorem = "Random string here" 
bar.ipsum = "But random doesn't exist, right ?" 
bar.dolor = 42 
bar.sit = 1337 

bar.name = "Foobar_" & var 'integer/string/variable here (lets say var is "Cookie") 

所以,當我想使用結構,我可以做這樣的事情:

msgbox("Secret cookie code =D >> " & callFunctionThatGetsStructureByName("Foobar_Cookie").sit.tostring) 

我得到callFunctionBlahblah.sit - > 1337

這可能嗎?我希望我的解釋清楚。如果我能得到任何幫助,謝謝:)

+0

聽起來像你只是想要一個鍵和你的結構字典。 – vcsjones 2014-11-20 18:47:12

+0

我要去尋找那個,謝謝,我會發布,如果我得到消息 – Riptide 2014-11-20 18:50:33

回答

0

這聽起來像你想要的只是一個Dictionary(Of String, foo)

只需創建一個新的字典:

Dim myDictionary = new Dictionary(Of String, foo)() 

然後,您可以通過按鍵添加項目:

dim bar as new foo 'Bar is temporary, it holds the information and then change its name [I know that's not working like that, imagine] (See below) 
bar.lorem = "Random string here" 
bar.ipsum = "But random doesn't exist, right ?" 
bar.dolor = 42 
bar.sit = 1337 

myDictionary.Add("Foobar_" & var, bar) 

然後你就可以在以後從字典檢索它就像這樣:

Dim retrieved = myDictionary("Foobar_Cookie") 
+0

這是聰明和完美的,正是我需要的,非常感謝;)這是你的餅乾;) – Riptide 2014-11-20 20:56:02