2014-11-20 68 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