i have problem about if else code in my program.
i also have made many changing in this code but still i didn't get the result that i want.

my program is about vending machine.

here is the code:

Private Sub btnpepsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpepsi.Click
        TextBox2.Text = "RM 2.50"
        Dim pepsi As Double
        Dim amount As Double

        If pepsi < 10 Then
            ListBox1.Items.Add("Pepsi")
            If displayText.Text > TextBox2.Text Then
                amount = displayText.Text - TextBox2.Text
                ListBox1.Items.Add("Your balance: " & amount & ".")
            End If
        Else
            TextBox2.Text = "Sold out"
            ListBox1.Items.Add("Sold out")
        End If
    End Sub

Recommended Answers

All 3 Replies

First off you need to find which types of variables to use.

You cannot subtract to string as if they were double

You want to create a variable for the cost of the pespi.

dim pepsicost as double= 2.50

TextBox2.Text="RM " & pepsicost

You need to make a function that will how many pepsi you have when you click on the Pepsi button because when you are creating "dim pepsi as double" you just declaring a variable but that variable does not have any value.

So make sure to compare DisplayCost>PepsiCost ...not strings

your variable amount(double) cannot subtract a string with another string and give you a double.

There are many tutorial on Youtube that can help you understand the difference in variable and how to send value over to other functions.

This part code :

If displayText.Text > TextBox2.Text Then ' Line 8
     amount = displayText.Text - TextBox2.Text ' Line 9

Your textbox2 value is String. you cannot subtract or compare it with a double value.
You need to convert it to double.

Exacttly. Text property is a String type. So you cannot use operatoros like <, > on them. YOu can only use these kind of operators on numbers (int, decimal, double, float,.. types).

Dim a As Double = Double.Parse(displayText.Text)
Dim b As Double = Double.Parse(TextBox2.Text)
If a > b Then
	amount = a - b
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.