2015-09-15 37 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

相關問題