PDA

View Full Version : Tech Help Cannot figure out why my Visual Basic code isn't working



McDouggal
2015-10-23, 09:57 PM
Here's the Code Behind: Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

' The btnCalculate will calculate first the square footage of mulch needed for the project, then the
' total cost of mulch and the additional $25 fee

' Variable Declaration
Dim MulchCost As Decimal = 9.87D
Dim InstallCost As Decimal = 25D
Dim Cost As Decimal
Dim Width As Decimal
Dim Length As Decimal
Dim Depth As Decimal

' Error if no data is entered
If textWidth.Text = "" OrElse textDepth.Text = "" OrElse textLength.Text = "" Then
MsgBox("Enter the Length, and Width of the area you would like covered in mulch, and the Depth to which you would like it to be covered to",
18, "Illegal Operation")


' Error if negative number is entered
ElseIf textWidth.Text = "-" OrElse textDepth.Text = "-" OrElse textLength.Text = "-" Then
MsgBox("Please enter a positive number", 18, "Illegal Operation")

End If

' Did User enter numeric Values for Width?
If IsNumeric(textWidth.Text) Then
Width = Convert.ToDecimal(textWidth.Text)
End If

' Did User enter numeric values for Length?
If IsNumeric(textLength.Text) Then
Length = Convert.ToDecimal(textLength.Text)
End If

' Did User enter numeric Values for Depth?
If IsNumeric(textDepth.Text) Then
Depth = Convert.ToDecimal(textDepth.Text)
End If
'Calculation of Cost Estimate
If Cost = Length * Width * Depth * MulchCost + InstallCost Then
lblAnswer1.Text = Cost.ToString("C")
End If

' I honestly cannot figure out why my code isn't displaying the answer
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
textDepth.Clear()
textWidth.Clear()
textLength.Clear()
textWidth.Focus()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Close()
End Sub

End Class



For the life of me, I cannot figure out why it isn't displaying the the Cost variable in lblAnswer1.

Jasdoif
2015-10-23, 10:11 PM
If Cost = Length * Width * Depth * MulchCost + InstallCost Then
lblAnswer1.Text = Cost.ToString("C")
End IfYou never actually assign a value to Cost, you're just doing that comparison. Cost is a Decimal, which I believe defaults to 0; so lblAnswer1 will only be set if your calculation results in 0.

I think what you're wanting is:

Cost = Length * Width * Depth * MulchCost + InstallCost
lblAnswer1.Text = Cost.ToString("C")

McDouggal
2015-10-23, 10:14 PM
you never actually assign a value to cost, you're just doing that comparison. Cost is a decimal, which i believe defaults to 0; so lblanswer1 will only be set if your calculation results in 0.

I think what you're wanting is:

cost = length * width * depth * mulchcost + installcost
lblanswer1.text = cost.tostring("c")

thank you!