Als je van het product een eigen datatype maakt, dan kun je alles daarop aanpassen. Ik heb je even een klein voorbeeld gemaakt. (twee listbox'en genaamd 'listbox1' en 'listbox2', en twee buttons genaamd 'button1' en 'button2')
Code:
Public Class Form1
Class Product
Private m_sNaam As String
Private m_iWaarde As Integer
Public Sub New()
m_sNaam = ""
m_iWaarde = 0
End Sub
Public Sub New(ByVal sNaam As String, ByVal iWaarde As Integer)
m_sNaam = sNaam
m_iWaarde = iWaarde
End Sub
Public Sub SetNaam(ByVal sNaam As String)
m_sNaam = sNaam
End Sub
Public Sub SetWaarde(ByVal iWaarde As Integer)
m_iWaarde = iWaarde
End Sub
Public Function GetNaam() As String
GetNaam = m_sNaam
End Function
Public Function GetWaarde() As Integer
GetWaarde = m_iWaarde
End Function
End Class
Private Function ZoekProduct(ByVal sNaam As String) As Product
For ix As Integer = 0 To UBound(productArray)
If productArray(ix).GetNaam() = sNaam Then
ZoekProduct = productArray(ix)
Exit Function
End If
Next
ZoekProduct = New Product("", 0)
End Function
Dim productArray() As Product = New Product() { _
New Product("Bruin brood", 40), _
New Product("Wit brood", 40), _
New Product("Aardappel", 20), _
New Product("Volkoren", 40)}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For ix As Integer = 0 To UBound(productArray)
ListBox1.Items.Add(productArray(ix).GetNaam())
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ListBox1.SelectedIndex <> -1 Then
Dim p As Product = ZoekProduct(ListBox1.SelectedItem.ToString())
Dim iCal As Integer = p.GetWaarde() / 100 * 50 ''50 is in dit geval 50 gram hardcoded
ListBox2.Items.Add(p.GetNaam() + "( 50 gr.) = " + CStr(iCal) + " Kcal")
Else
MessageBox.Show("blabla error")
End If
End Sub
End Class
Laatst bewerkt: