I'm having an issue in a large piece of code that I'm working on, so I created a short piece of code to show the issue. I created a class (stockstatus) that represents an inventory item . They have a part number (.part), a quantity (.qty), and a cost (.cost). I created a list (ssr) of that class to represent the entire inventory. I'm now trying to search that inventory for a specific part number, but I'm having trouble.
If I try ssr.IndexOf, it expects a stockstatus to be passed to the IndexOf function. If I create a stockstatus with an underlying .part that I initialize to the second part number ("CP0065-10N-S"), the IndexOf function always returns -1. I'm thinking that the .qty and .cost also have to match for IndexOf to return the correct index, but I have no way of knowing those values in the code that I'm working on. I need to be able to find an index based solely on searching for a .part. What is the simplest way to do this?
Public Class stockstatus
Public part As String
Public qty As Double
Public cost As Double
End Class
Public Partial Class MainForm
Public ssr As New List(Of stockstatus)
Public Sub New()
Me.InitializeComponent()
End Sub
Sub MainFormLoad(sender As Object, e As EventArgs)
ssr = getSSR()
Dim ss As New stockstatus
ss.part = "CP0065-10N-S"
MsgBox(ssr.IndexOf(ss)) 'returns -1. i want it to return index 1
End Sub
Public Function getSSR() As List(Of stockstatus)
Dim s As New List(Of stockstatus)
Dim ss As New stockstatus
ss.part = "CP0065-0.1U-S"
ss.qty = 1000.0R
ss.cost = 0.001R
s.Add(ss)
ss.part = "CP0065-10N-S"
ss.qty = 2000.0R
ss.cost = 0.002R
s.Add(ss)
Return s
End Function
End Class