简体   繁体   English

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

[英]Using Objects in a dictionary in vb.net

This is how I Check if my dictionary occurences already contains a key and a value and assign keys and values to it... but I need another value, so how do I use the object here? 这是我检查我的字典里occurences已经包含一个键和一个值,并指定键和值...但我需要另一种价值,所以我怎么在这里使用的对象?

Occurences.Add(xRows.Cells(4).Value.ToString(), Object)

Somehow like this: 不知何故这样:

occurences(xRows.Cells(4).Value.ToString()) = Double.Parse(occurences(xRows.Cells(4).Value.ToString()).ToString()) + <"1st object value here">)

But this is how I currently do it which only has a key and a value but I need something like key, object which has (value, value), something like that: 但这就是我目前这样做的方式,它只有一个键和一个值,但我需要像key,object这样的东西(value,value),类似的东西:

Actual code: 实际代码:

If (occurences.ContainsKey(xRows.Cells(4).Value.ToString())) Then
    occurences(xRows.Cells(4).Value.ToString()) = Double.Parse(occurences(xRows.Cells(4).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(7).Value.ToString())
Else
    occurences.Add(xRows.Cells(4).Value.ToString(), Double.Parse(xRows.Cells(7).Value.ToString()))
End If

Next

Then i have another code for insert where i need to use the 2nd value of the object. 然后我有另一个插入代码,我需要使用对象的第二个值。

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

Do the following: 请执行下列操作:

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

Then reference it via occurences("keynamegoeshere").Quantity for use in your SQL. 然后通过出现occurences("keynamegoeshere").Quantity引用它occurences("keynamegoeshere").Quantity在SQL中使用的occurences("keynamegoeshere").Quantity

I'm not sure I understand your question fully, but if I do I would suggest a different approach: 我不确定我完全理解你的问题,但如果我这样做,我会提出一个不同的方法:

Why not make a class that holds your values, then add your class to the dictionary - or - create a dictionary of the class type: 为什么不创建一个包含值的类,然后将类添加到字典中 - 或者 - 创建类类型的字典:

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

Dim myClassInstance as New myClass
'initialize fields
occurences.Add(key, myClassInstance)

To retrieve get the value as an object and use if typeof value is myclass then - if you choose object as type for the dictionary. 要检索获取值作为对象,并使用if typeof value is myclass then - 如果您选择object作为字典的类型。

If you choose object you can store different classes and types of course, but the cost is the casting. 如果选择对象,则可以存储不同的类和类型,但成本是铸造。 If possible make a dictionary of the type you will use to avoid casting. 如果可能的话,制作一个类型的字典,用于避免投射。

Private myDictionary as New Dictionary(Of String, myClass)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM