0

I have an existing VB.net program (using visual studio 2005) that uses class InventoryLocation, and has a child class InventoryItem. I would like to revise this code to use my new class InventorySequence, which has a new primary key, includes the fields from the other classes, but does not have any child classes.

The main part of my issue is that I do not understand "KeyValuePair". From what I understand, KeyValuePair has one key and one value. In InventorySequence, I have one key, but three values. Please advise - what can I use instead of KeyValuePair? See existing code I would like to change at the bottom:

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
user3637652
  • 283
  • 1
  • 3
  • 12
  • The `value` can be a `List(Of T)`. – OneFineDay Mar 10 '17 at 16:45
  • 1
    You probably shouldn't be [inheriting from a list](http://stackoverflow.com/q/21692193/719186). Also, use Properties in your classes instead of fields. What's with the UInteger? Just use Integer. – LarsTech Mar 10 '17 at 16:46
  • You could write a collection class to implement things like `ItemsByKey()` . – Ňɏssa Pøngjǣrdenlarp Mar 10 '17 at 16:59
  • The `value` can also be a `Structure` or `Class` containing three items (which is probably better than a `List(Of T)` in this case because it looks as though the three values have different types). In more recent versions of the BCL, you could also use a three-member `Tuple` to wrap the three items, but the advantage of a `Structure` or `Class` is that they retain meaningful names instead of `Item1`, `Item2`, and `Item3`. – Craig Mar 10 '17 at 19:31

1 Answers1

0

Some good advice was given in the comments - thanks to all who posted.

I resolved this issue by created two classes, one was a Sequence class with just the ID and a SubClass value. Then, the second class was the ID and the other columns I needed. I stopped using the previously existing Class logic, and created similar logic based on my new Classes.

When using KeyValuePair, I used this Sequence ID as the key, and the subClass as the value, and then simply populated my columns from data in the subClass.

user3637652
  • 283
  • 1
  • 3
  • 12