2015-09-15 19 views
0

如何添加另一个值/参数/变量到这个例子?如何在VB.Net中为此示例添加另一个值?

Dim pricesList As New List(Of KeyValuePair(Of String, String)) 
pricesList.AddRange({New KeyValuePair(Of String, String)("$5", "1"), 
        New KeyValuePair(Of String, String)("$3", "2")}) 

For Each item As KeyValuePair(Of String, String) In pricesList 
    Console.WriteLine(String.Format("Price: {0}, Value {1}", item.Key, item.Value)) 
Next item 

所以,我得到这样的

Dim pricesList As New List(Of KeyValuePair(Of String, String)) 
pricesList.AddRange({New KeyValuePair(Of String, String)("$5", "1","1","1"), 
        New KeyValuePair(Of String, String)("$3", "2","2","2")}) 

For Each item As KeyValuePair(Of String, String) In pricesList 
    Console.WriteLine(String.Format("Price: {0}, Value {1}, Value {1}, Value {1}, Value {1}", item.Key, item.Value)) 
Next item 

回答

0

你不能传递一个数组String.Format,并有阵列的不同元素在你的字符串替换不同的参数。你可以做如下的事情。请注意,我将List (Of KeyValuePair)更改为Dictionary,因为这似乎更有意义。由于键和值似乎都是整数,所以您可能还想使用整数(或某种数字)值而不是字符串。

[编辑]在回复评论时,添加了一个声明,显示使用TextBoxes中的值向Dictionary添加项目的示例。

Dim pricesList As New Dictionary(Of String, String()) _ 
    From {{"$5", {"1", "1", "1"}}, {"$3", {"2", "2", "2"}}} 

pricesList.Add(PriceTextBox.Text, {V1TextBox.Text, V2TextBox.Text, V3TextBox.Text}) 

For Each item As KeyValuePair(Of String, String()) In pricesList 
    Console.WriteLine(String.Format("Price: {0}, Value {1}, Value {2}, Value {3}", item.Key, item.Value(0), item.Value(1), item.Value(2))) 
Next item 
+0

我该如何为此添加一些东西? – Hanselman

+0

如果你的意思是你如何在词典中添加第三个关键字,将From子句改为From {{“$ 5”,{“1”,“1”,“1”}},{“$ 3”, {“2”,“2”,“2”}},{“$ 1”,{“3”,“3”,“3”}}}'。如果你的意思是你如何在每个字典值中有四个值而不是三个值,只需在数组中添加另一个元素即可。例如,将第一个Dictionary项从'{“$ 5”,{“1”,“1”,“1”}}'更改为'{“$ 5”,{“1”,“1”,“1”, “1”}}'等等,并将第四项添加到您的'String.Format'语句中。 – Blackwood

+0

ahh :)我的意思是我怎么可以在我的脚本中的其他地方添加它... 完整说明...我需要从文本框中获取值...并将其添加到每个按钮按..然后在每个循环工作atm我需要回声他们以后... – Hanselman

相关问题