在vb.net中动态创建结构名称

时间:2014-11-20 18:43:59

标签: vb.net dynamic structure

我想用字符串创建一个动态结构名称,这是我的结构:

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

这可能吗?我希望我的解释清楚。如果我能得到任何帮助,谢谢:)

1 个答案:

答案 0 :(得分: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")