Asp.net 在vb.net中使用字典中的对象

Asp.net 在vb.net中使用字典中的对象,asp.net,vb.net,winforms,dictionary,datagridview,Asp.net,Vb.net,Winforms,Dictionary,Datagridview,这就是我如何检查我的字典occurrences是否已经包含键和值,并为其分配键和值。。。但是我需要另一个值,那么我如何使用这个对象呢 Occurences.Add(xRows.Cells(4).Value.ToString(), Object) 不知怎的,是这样的: occurences(xRows.Cells(4).Value.ToString()) = Double.Parse(occurences(xRows.Cells(4).Value.ToString()).ToString())

这就是我如何检查我的字典
occurrences
是否已经包含键和值,并为其分配键和值。。。但是我需要另一个值,那么我如何使用这个对象呢

Occurences.Add(xRows.Cells(4).Value.ToString(), Object)
不知怎的,是这样的:

occurences(xRows.Cells(4).Value.ToString()) = Double.Parse(occurences(xRows.Cells(4).Value.ToString()).ToString()) + <"1st object value here">)
然后我有另一个插入代码,我需要使用对象的第二个值

Using commm As New MySqlCommand()
    With commm
         .Parameters.Clear()
         .Parameters.AddWithValue("@iBrnchCde", cmbBrnchCode.Text)
         .Parameters.AddWithValue("@iFCode", pair.Key)
         .Parameters.AddWithValue("@iDesc", <"2nd OBJECT VALUE HERE"> )
         .Parameters.AddWithValue("@iQty", pair.Value)
         .CommandText = oInsertString
         .Connection = _conn
         .CommandType = CommandType.Text
     End With
     commm.ExecuteNonQuery()
End Using
使用commm作为新的MySqlCommand()
用逗号
.Parameters.Clear()
.Parameters.AddWithValue(“@iBrnchCde”,cmbBrnchCode.Text)
.Parameters.AddWithValue(“@iFCode”,pair.Key)
.Parameters.AddWithValue(“@iDesc”,)
.Parameters.AddWithValue(“@iQty”,pair.Value)
.CommandText=oInsertString
.Connection=_conn
.CommandType=CommandType.Text
以
commm.ExecuteNonQuery()
终端使用

我不确定我是否完全理解您的问题,但如果我理解,我会建议一种不同的方法:

为什么不创建一个保存您的值的类,然后将您的类添加到字典中,或者创建一个类类型的字典:

Public myClass
    Public Code As String
    Public Desc as String
    ....
End Class

Dim myClassInstance as New myClass
'initialize fields
occurences.Add(key, myClassInstance)
要检索,请将值作为对象获取并使用
,如果值的类型为myclass,则使用
-如果选择对象作为字典的类型

如果选择对象,当然可以存储不同的类和类型,但成本是铸造成本。如果可能的话,制作一本你将使用的类型的字典,以避免强制转换

Private myDictionary as New Dictionary(Of String, myClass)
请执行以下操作:

Class CustomRowObject
    Public Property Name() As String
    Public Property Quantity() As Double
    Public Property Description() As String
End Class


If (occurences.ContainsKey(xRows.Cells(4).Value.ToString())) Then
    occurences(xRows.Cells(4).Value.ToString()).Quantity = Double.Parse(occurences(xRows.Cells(4).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(7).Value.ToString())
Else
    occurences.Add(xRows.Cells(4).Value.ToString(),New CustomRowObject With { .Name = a.Cells("ProductName").Value.ToString(),.Description = .Cells("ProductDesc").Value.ToString(), .Quantity = a.Cells("Quantity").Value.ToString()})
End If

Next

然后通过
occurrences(“keynamegoesher”).Quantity引用它,以便在SQL中使用。

问题没有意义。@MitchWheat我在问如何使用字典中的对象,它的哪一部分没有意义?谢谢你的回答,是的,我现在正在尝试这样做谢谢:不要重复
xRows.Cells(4.Value.ToString()
4次,您可以将其分配给变量并使用该变量。代码将更容易阅读@请再帮我一点忙。:)
Class CustomRowObject
    Public Property Name() As String
    Public Property Quantity() As Double
    Public Property Description() As String
End Class


If (occurences.ContainsKey(xRows.Cells(4).Value.ToString())) Then
    occurences(xRows.Cells(4).Value.ToString()).Quantity = Double.Parse(occurences(xRows.Cells(4).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(7).Value.ToString())
Else
    occurences.Add(xRows.Cells(4).Value.ToString(),New CustomRowObject With { .Name = a.Cells("ProductName").Value.ToString(),.Description = .Cells("ProductDesc").Value.ToString(), .Quantity = a.Cells("Quantity").Value.ToString()})
End If

Next