lstRecipe.Text
will reference the SELECTED item in the recipe list box, which is not the same as the one you just added. Even if there was something selected, you would just add that many calories each time some other item was added. Since you are posting (Quantity & " " & lstIngredients.Text)
to Recipe, the text would never match the Ingredient only text (and should be Quantity.ToString
).
lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)
' this assumes the quoted text is exactly
' what they contain. we cant see that, but
' lstRECIPE would be something like '4 Eggs(each)'
' this will also break if you change the text like
' add a space before the parens 'Eggs (each)'
' which is another reason a class is a much better way
If lstIngredients.Text = "Eggs(each)" Then
TotalCalories += Quantity * 72
ElseIf lstIngredients.Text = "Flour(cups)" Then
TotalCalories += Quantity * 455
ElseIf lstIngredients.Text = "Milk(cups)" Then
TotalCalories += Quantity * 86
ElseIf lstIngredients.Text = "Sugar(cups)" Then
TotalCalories += Quantity * 774
ElseIf lstIngredients.Text = "Butter(tablespoons)" Then
TotalCalories += Quantity * 102
End If
Cleaner:
lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)
Select Case lstIngredients.Text
Case "Eggs(each)"
TotalCalories += Quantity * 72
Case "Flour(cups)"
TotalCalories += Quantity * 455
Case "Milk(cups)" Then
TotalCalories += Quantity * 86
Case "Sugar(cups)"
TotalCalories += Quantity * 774
Case "Butter(tablespoons)"
TotalCalories += Quantity * 102
End Select
Since the Item, Quantity and Calories are all needed in different spots, this is ideal for a Class. Have these come up yet, because this the solution screams 'use a class'.