VB.net 2005使用Class和KeyValuePair的问题

时间:2017-03-10 16:42:00

标签: vb.net

我有一个使用类InventoryLocation的现有VB.net程序(使用visual studio 2005),并且有一个子类InventoryItem。我想修改此代码以使用我的新类InventorySequence,它具有一个新的主键,包括其他类的字段,但没有任何子类。

我的问题的主要部分是我不理解“KeyValuePair”。据我所知,KeyValuePair有一个键和一个值。在InventorySequence中,我有一个键,但有三个值。请指教 - 我可以使用什么而不是KeyValuePair?请参阅我想在底部更改的现有代码:

Public Class InventoryLocation ' original coding
    Public Location As String = ""
    Public Items As InventoryItems
    Public Sub New(ByVal location__1 As String)
        Location = location__1
        Items = New InventoryItems()
    End Sub
End Class
Public Class InventoryLocations
    Inherits SortedList(Of String, InventoryLocation)
End Class
'----
Public Class InventoryItem  ' original coding
    Public Location As InventoryLocation
    Public Quantity As UInteger = 0
    Public Barcode As String = ""
    Public Sub New(ByVal location__1 As InventoryLocation, ByVal quantity__2 As UInteger, ByVal barcode__3 As String)
        Location = location__1
        Quantity = quantity__2
        Barcode = barcode__3
    End Sub
End Class
Public Class InventoryItems
    Inherits SortedList(Of String, InventoryItem)
End Class
'----
Public Class InventorySequence ' my coding - need new itemseq to be the primary key
    Public ItemSeq As UInteger = 0
    Public Location As String = ""
    Public Quantity As UInteger = 0
    Public Barcode As String = ""
    Public Sub New(ByVal itemseq__4 As UInteger, ByVal location__1 As String, ByVal quantity__2 As UInteger, ByVal barcode__3 As String)
        ItemSeq = itemseq__4
        Location = location__1
        Quantity = quantity__2
        Barcode = barcode__3
    End Sub
End Class
Public Class InventorySequences
    Inherits SortedList(Of String, InventorySequence)
End Class
'---------------------------- How do I replace this code to do the same thing but use my InventorySequence class?
Dim index As Integer = 0
For Each kvpLocations As KeyValuePair(Of String, InventoryLocation) In Inventory.Locations
    Dim searchLocation As InventoryLocation = kvpLocations.Value
    If searchLocation.Location = inventoryItem.Location Then
        For Each kvpItems As KeyValuePair(Of String, InventoryItem) In searchLocation.Items
            Dim searchItem As InventoryItem = kvpItems.Value
            If searchItem.Barcode = inventoryItem.Barcode Then
                Exit For
            End If
            index += 1
        Next
        Exit For
    Else
        index += searchLocation.Items.Count
    End If
Next

1 个答案:

答案 0 :(得分:0)

评论中给出了一些好的建议 - 感谢所有发帖的人。

我通过创建两个类来解决这个问题,一个是一个只有ID和一个SubClass值的Sequence类。然后,第二个类是我需要的ID和其他列。我停止使用以前存在的类逻辑,并根据我的新类创建了类似的逻辑。

使用KeyValuePair时,我使用此序列ID作为键,使用subClass作为值,然后简单地从subClass中的数据填充我的列。