哈希表条目 -> 使用"For Each 循环"vb.net 的组合框



有人给了我一些代码,试图从哈希表中取出值并将它们加载到组合框中。

        Dim lstProductItems As List(Of Hashtable)
    ' Fill the product combobox with details from the product table.
    lstProductItems = oPurchaseOrderController.getAllProducts()
    For Each product In lstProductItems
        'cboItems.Items.Add(product("SKU") & " - " & product("ProductName"))
        cboItems.Items.Add(product("SKU") & " - " & product("ProductName"))
    Next

这是提供的代码,它加载一个哈希表,然后使用for每个循环将它们加载到组合框项列表中。这显然行不通,因为'product'是一个完全未声明的变量。

然而,当我将'product'更改为'product as string'时,它会给我抛出错误:类型"System.Collections"的值。哈希表'不能转换为'字符串'。"

我不知道如何改变这段代码,使其工作,这种错误的方法从哈希表绘图出现在我提供的代码中的许多其他领域,显然其他人已经得到它的工作。

谢谢你的帮助

下面是如何在每个循环中使用Hashtable的c#代码。

    Hashtable hashtable = new Hashtable();
    hashtable[1] = "One";
    hashtable[2] = "Two";
    hashtable[13] = "Thirteen";
    foreach (DictionaryEntry entry in hashtable)
    {
        Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
    }

详情请浏览http://www.dotnetperls.com/hashtable

我想知道你是否真的需要一个列表作为你的HashTable

Dim hashTable As New HashTable()
hashTable = oPurchaseOrderController.getAllProducts()
For Each item As DictionaryEntry In hashTable
    cboItems.Items.Add(item.Key + " - " + item.Value)
Next

最新更新