Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Code not working as expected - implicit conversion?
 

Code not working as expected - implicit conversion?

I am working on a homework assignment for Visual Basic using VB 2008. The application is a cash register. My problem is with the enter button click event handler.

Here is the code for the enter button as I have it right now:

Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click

'declare variable

Dim subtotal As Decimal

'add current amount to subtotal then clear price text box

subtotal = Val(subtotalResultLabel.Text) + Val(priceTextBox.Text)

subtotalResultLabel.Text = subtotal

priceTextBox.Clear()

End Sub


The goal here is to add the value from the priceTextBox to the value from the subtotalResultLabel and display the new subtotal in the subtotalResultLabel then clear the priceTextBox. Should be simple.

I have the line "commented out" that should convert the variable "subtotal" toa string in the currency format and display in a resultlabel. My problem is that with the code as it is right now, the calculation is correct -price entered adds to the subtotal and displays in the result label - but the format is not in currency.

When I "comment out" this line:

subtotalResultLabel.Text = subtotal

and use the other line instead, the result is displayed in currency, but the calculation doesn't work. I must be missing something with regards to implicit conversions... I know there has to be a simple answer - but so far it has escaped me. Anyone able to clue me in, please?

Thank you in advance for any help you may offer.

The complete code as I have it now is below

Thank you in advance for any help you may offer.

The complete code as I have it now is below

Public

Class CashRegisterForm

Private Sub zeroButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zeroButton.Click

priceTextBox.Text &=

"0" 'concatenate "0" to display

End Sub 'zero button click

Private Sub oneButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oneButton.Click

priceTextBox.Text &=

"1" 'concatenate "1" to display

End Sub 'one button click

Private Sub twoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles twoButton.Click

priceTextBox.Text &=

"2" 'concatenate "2" to display

End Sub 'two button click

Private Sub threeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles threeButton.Click

priceTextBox.Text &=

"3" 'concatenate "3" to display

End Sub ' three button click

Private Sub fourButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fourButton.Click

priceTextBox.Text &=

"4" 'concatenate "4" to display

End Sub ' four button click

Private Sub fiveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fiveButton.Click

priceTextBox.Text &=

"5" 'concatenate "5" to display

End Sub ' five button click

Private Sub sixButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sixButton.Click

priceTextBox.Text &=

"6" 'concatenate "6" to display

End Sub ' six button click

Private Sub sevenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sevenButton.Click

priceTextBox.Text &=

"7" 'concatenate "7" to display

End Sub 'seven button click

Private Sub eightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eightButton.Click

priceTextBox.Text &=

"8" 'concatenate "8" to display

End Sub ' eight button click

Private Sub nineButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nineButton.Click

priceTextBox.Text &=

"9" 'concatenate "9" to display

End Sub ' nine button click

Private Sub pointButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pointButton.Click

priceTextBox.Text &=

"." 'concatenate "." to display

End Sub 'point button click

Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click

'declare variable

Dim subtotal As Decimal

'add current amount to subtotal then clear price text box

subtotal = Val(subtotalResultLabel.Text) + Val(priceTextBox.Text)

subtotalResultLabel.Text = subtotal

'subtotalResultLabel.Text = String.Format("{0:C}", subtotal)

priceTextBox.Clear()

End Sub 'enter button click

Private Sub totalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalButton.Click

'declare variables

Dim subtotal As Decimal

Dim tax As Decimal

Dim total As Decimal

subtotal = Val(subtotalResultLabel.Text)

Select Case subtotal

Case Is < 100

tax = 0.1 * subtotal

Case 100 To 500

tax = 0.075 * subtotal

Case Else

tax = 0.05 * subtotal

End Select

'display tax

taxResultLabel.Text =

String.Format("{0:C}", tax)

'display total

total = (subtotal + tax)

totalResultLabel.Text =

String.Format("{0:C}", total)

End Sub 'total button click

Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click

'clear user input, subtotal, tax and total

priceTextBox.Clear()

subtotalResultLabel.Text =

""

taxResultLabel.Text =

""

totalResultLabel.Text =

""

End Sub 'clear button click

Private Sub deleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteButton.Click

'delete user input

priceTextBox.Clear()

End Sub 'delete button click

End

Class ' CashRegisterForm

kandeannasas  15 hours 35 minutes ago



Dont use "Val" !!

This will work

subtotal = CDec(TextBox1.Text) + CDec(TextBox2.Text)

TextBox3.Text = String.Format("{0:C}", subtotal)

Crazypennie  7 hours 12 minutes ago

I see copying and pasting the code into this post from the VB Express editordidn't work too well... am trying again from notepad...

Public Class CashRegisterForm

Private Sub zeroButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zeroButton.Click
priceTextBox.Text &= "0" 'concatenate "0" to display
End Sub 'zero button click

Private Sub oneButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oneButton.Click
priceTextBox.Text &= "1" 'concatenate "1" to display
End Sub 'one button click

Private Sub twoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles twoButton.Click
priceTextBox.Text &= "2" 'concatenate "2" to display
End Sub 'two button click

Private Sub threeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles threeButton.Click
priceTextBox.Text &= "3" 'concatenate "3" to display
End Sub ' three button click

Private Sub fourButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fourButton.Click
priceTextBox.Text &= "4" 'concatenate "4" to display
End Sub ' four button click

Private Sub fiveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fiveButton.Click
priceTextBox.Text &= "5" 'concatenate "5" to display
End Sub ' five button click

Private Sub sixButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sixButton.Click
priceTextBox.Text &= "6" 'concatenate "6" to display
End Sub ' six button click

Private Sub sevenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sevenButton.Click
priceTextBox.Text &= "7" 'concatenate "7" to display
End Sub 'seven button click

Private Sub eightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eightButton.Click
priceTextBox.Text &= "8" 'concatenate "8" to display
End Sub ' eight button click

Private Sub nineButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nineButton.Click
priceTextBox.Text &= "9" 'concatenate "9" to display
End Sub ' nine button click

Private Sub pointButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pointButton.Click
priceTextBox.Text &= "." 'concatenate "." to display
End Sub 'point button click

Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click

'declare variable
Dim subtotal As Decimal

'add current amount to subtotal then clear price text box
subtotal = Val(subtotalResultLabel.Text) + Val(priceTextBox.Text)
subtotalResultLabel.Text = subtotal

'subtotalResultLabel.Text = String.Format("{0:C}", subtotal)

priceTextBox.Clear()

End Sub 'enter button click

Private Sub totalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalButton.Click
'declare variables
Dim subtotal As Decimal
Dim tax As Decimal
Dim total As Decimal

subtotal = Val(subtotalResultLabel.Text)

Select Case subtotal
Case Is < 100
tax = 0.1 * subtotal
Case 100 To 500
tax = 0.075 * subtotal
Case Else
tax = 0.05 * subtotal
End Select

'display tax
taxResultLabel.Text = String.Format("{0:C}", tax)

'display total
total = (subtotal + tax)
totalResultLabel.Text = String.Format("{0:C}", total)

End Sub 'total button click

Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click

'clear user input, subtotal, tax and total
priceTextBox.Clear()
subtotalResultLabel.Text = ""
taxResultLabel.Text = ""
totalResultLabel.Text = ""

End Sub 'clear button click

Private Sub deleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteButton.Click

'delete user input
priceTextBox.Clear()

End Sub 'delete button click
End Class ' CashRegisterForm

kandeannasas  14 hours 48 minutes ago
Try:

subtotalResultLabel.Text = subtotal.ToString("C")

jgalley  12 hours 24 minutes ago

Even when I turn option strict off to allow the implicit conversion...it works fine here using the String.Format line.

Val is overloaded, but...it returns Doubles. Sometimes, when you are changing types, and converting...the compiler gets confused. The compiler is not free tro make any sort of assumption about your intent, so I like to make things very clear to the compiler by using the conversion functions liberally.

CDec is one that makes it clear to the compiler you mean to provide a Decimal type.

e.g. subtotal = CDec(Val(subtotalResultLabel.Text))

I'm not saying that is your answer, but...I think your answer is close to that.

jinzai  12 hours 3 minutes ago
thanks much for your reply but thisdidn't work...
kandeannasas  8 hours 45 minutes ago
Thank you for your ideas.
I thought this might be the answer - but it didn't work either... still scratching my head...
kandeannasas  8 hours 42 minutes ago



Dont use "Val" !!

This will work

subtotal = CDec(TextBox1.Text) + CDec(TextBox2.Text)

TextBox3.Text = String.Format("{0:C}", subtotal)

Crazypennie  7 hours 12 minutes ago
HORRAY!! it worked!!!
I learned something new!
Thanks SO much!
kandeannasas  4 hours 52 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• menu item selected to open same form
• Generics Question
• VB.NET Express 2005 VS. VB.NET 2005
• Converting Textbox data with a seperator into an array
• Retrieving unique value combinations from values in multiple arrays
• Registry 64-bit value to Date
• not getting the output on my screen help
• Problem reading tooltips on a menustrip
• Updating version of MS Access database
• My Spider does not read Javascript