# Calculator Application HELP

• ### Question

• Hello,

I am new and creating a price calculator. The user will enter a product from a combobox. They will enter the weight through a textbox, and they will then enter whether the price is for a consumer, or a landscaper (who would get a discounted rate) using a combobox.

Needless to say, its not working out. I have tried to do things many ways, but currently all i get is 0 for everything but the weight (which works) and the customer type which is blank.

Current code, more code would be added for each product

Public Class Price_Calculator
Dim Product As String
Dim PricePerTon As Double
Dim Weight As Double
Dim CustomerType As String
Dim Price As Double
'SPACER
'CLEAR BUTTON
'SPACER
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
Me.cboProducts.Text = Nothing
Me.txtWeight.Text = Nothing
Me.cboCustomerType.Text = Nothing
'SPACER
Me.lblProduct.Text = Nothing
Me.lblPricePerTon.Text = Nothing
Me.lblWeight.Text = Nothing
Me.lblCustomerType.Text = Nothing
Me.lblPrice.Text = Nothing
End Sub
'SPACER
'CALCULATE BUTTON
'SPACER
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Product = Val(Me.cboProducts.Text)
Weight = Val(Me.txtWeight.Text)
'SPACE
If Product = "All bagged Colorado River Rock" Then
If CustomerType = "Consumer" Then
PricePerTon = "180"
ElseIf CustomerType = "Landscaper" Then
PricePerTon = "160"
End If
Weight = Weight
Price = Weight / 2000 * PricePerTon
ElseIf Product = "Product B" Then
If CustomerType = "Consumer" Then
PricePerTon = "250"
ElseIf CustomerType = "Landscaper" Then
PricePerTon = "200"
End If
Weight = Weight
Price = Weight / 2000 * PricePerTon
End If
'
Me.lblProduct.Text = Product
Me.lblPricePerTon.Text = PricePerTon
Me.lblWeight.Text = Weight & "lbs"
Me.lblCustomerType.Text = CustomerType
Me.lblPrice.Text = "\$" & Price
End Sub
End Class

• Edited by Wednesday, August 30, 2017 3:52 AM
Wednesday, August 30, 2017 3:42 AM

• Product and PricePerTon are both alphabetical characters, what would be the best way to declare them?

A combobox supports the SelectedIndex property that gives you the index of the item in the Items collection that was selected.  If you use that index instead of the text property, then the text could change, but your code would still work.

If cboProducts.SelectedIndex = 0 Then 'User selected the first item in the combobox list of items

But that's not your current problem.   How does CustomerType get set to a value, so that you can test it properly with the If statement to get the right product price?

• Marked as answer by Friday, September 1, 2017 2:28 AM
Wednesday, August 30, 2017 12:08 PM
• I have put in:

CustomerType= Val(Me.cboCustomerType.Text)

under btnCalculate

That implies that the customer type is selected by the user from a series of customer types listed in a combobox (cboCustomerType).  You aren't interested in the text - that is there simply to assist the user in making their selection.  You are interested in identifying which item they selected.  That is the SelectedIndex property of the combobox. When you make the same change for the Product then the code becomes:

If cboProducts.SelectedIndex = 0 Then
If cboCustomerType.SelectedIndex = 0 Then
PricePerTon = "180"
ElseIf cboCustomerType.SelectedInex = 1 Then
PricePerTon = "160"
End If

That might solve the initial problem, IF that problem was that your text strings did not match the actual text in the comboboxe - for instance if the combobox text was "All Bagged Colorado River Rock" rather than "All bagged Colorado River Rock" which you were testing for.

But the other possibility was that the user had not selected anything from one of the comboboxes, in which case your result would be nothing.

• Marked as answer by Friday, September 1, 2017 2:28 AM
Wednesday, August 30, 2017 9:09 PM

### All replies

• Needless to say, its not working out. I have tried to do things many ways, but currently all i get is 0 for everything but the weight (which works) and the customer type which is blank.

The most likely reason for that result is that customer type does not match any of the string literals that you have used.  There is no indication of where this variable comes from, but a problem with setting that variable will mean that neither branch of the If statement will be true, and no prive per ton will be assigned.

It is generally not a good idea to rely on matching text strings.  For instance, if customer type is selected from a combobox then you should be using the combobox collection index number, not the item text.

The line

Weight = Weight

Does nothing and should be removed.

• Edited by Wednesday, August 30, 2017 4:45 AM sp
• Marked as answer by Friday, September 1, 2017 2:27 AM
• Unmarked as answer by Friday, September 1, 2017 2:27 AM
Wednesday, August 30, 2017 4:45 AM
• Thank you,

Product and PricePerTon are both alphabetical characters, what would be the best way to declare them?

Wednesday, August 30, 2017 11:58 AM
• Product and PricePerTon are both alphabetical characters, what would be the best way to declare them?

A combobox supports the SelectedIndex property that gives you the index of the item in the Items collection that was selected.  If you use that index instead of the text property, then the text could change, but your code would still work.

If cboProducts.SelectedIndex = 0 Then 'User selected the first item in the combobox list of items

But that's not your current problem.   How does CustomerType get set to a value, so that you can test it properly with the If statement to get the right product price?

• Marked as answer by Friday, September 1, 2017 2:28 AM
Wednesday, August 30, 2017 12:08 PM
• Thank you, I will try that.

I have put in:

CustomerType= Val(Me.cboCustomerType.Text)

under btnCalculate

Wednesday, August 30, 2017 6:31 PM
• Avoid the Val it is a kind of gambling version from Bills Gates Basic.

Convert the textbox to the value type you use after cheking if it is numeric.

Samples are

dim DecimalValue as decimal = 0.0
if IsNumeric(TextBox1.Text) then
DecimalValue = CDec(TextBox1.Text)
End if

or the one we got because this was not in C#

dim DecimalValue as decimal = 0.0
Decimal.TryParse(TextBox1.Text,DecimalValue)

Take the one you like, I don't like the second because it is passing a value to a sub, but as I said, personal preference.

Success
Cor

Wednesday, August 30, 2017 6:40 PM
• Is there a way to use something other than a string?
Wednesday, August 30, 2017 6:49 PM
• Is there a way to use something other than a string?

I don't know if it make sense to help you. I took some time to give you 2 options and then you reply with this.

Yest you can use a numeric textbox

Success
Cor

Wednesday, August 30, 2017 6:53 PM
• Is there a way to use something other than a string?

I don't know if it make sense to help you. I took some time to give you 2 options and then you reply with this.

Yest you can use a numeric textbox

Success
Cor

It isn't that I don't appreciate the help, it is just that if possible, my personal preference would be to use the products name rather than index. In the future, should I need to add products, I would add products in a certain order. (I don't want it Product A, Product D, Product F, Product B, etc.)

Also, when I asked the question, I had not seen your initial reply. I sent the first response and shortly after sent the other without refreshing the page. I am now seeing this as I am responding.

Wednesday, August 30, 2017 9:08 PM
• I have put in:

CustomerType= Val(Me.cboCustomerType.Text)

under btnCalculate

That implies that the customer type is selected by the user from a series of customer types listed in a combobox (cboCustomerType).  You aren't interested in the text - that is there simply to assist the user in making their selection.  You are interested in identifying which item they selected.  That is the SelectedIndex property of the combobox. When you make the same change for the Product then the code becomes:

If cboProducts.SelectedIndex = 0 Then
If cboCustomerType.SelectedIndex = 0 Then
PricePerTon = "180"
ElseIf cboCustomerType.SelectedInex = 1 Then
PricePerTon = "160"
End If

That might solve the initial problem, IF that problem was that your text strings did not match the actual text in the comboboxe - for instance if the combobox text was "All Bagged Colorado River Rock" rather than "All bagged Colorado River Rock" which you were testing for.

But the other possibility was that the user had not selected anything from one of the comboboxes, in which case your result would be nothing.

• Marked as answer by Friday, September 1, 2017 2:28 AM
Wednesday, August 30, 2017 9:09 PM
• Alright, thank you for deeper explanation. I will start building the code. Thank you for the help, I plan to build an inventory/sales system after this. With all the help, I may have to put your name on it..
Wednesday, August 30, 2017 10:26 PM
• In the future, should I need to add products, I would add products in a certain order. (I don't want it Product A, Product D, Product F, Product B, etc.)

The conversion to numeric isn't for the products - it's for user data entry that must be a number because it is going to be used in a mathematical calculation, such as the weight.

The product name should be a string because that's what the user needs to see, but it is not part of your code and does not need to be converted to a number.

Wednesday, August 30, 2017 10:49 PM