r/visualbasic • u/Lord_Ewok • Nov 02 '18
Using If Else to Calcuate Wages .
Hey guys so am making a wage Calc and i am using If Else to perform the calculations if Hours are above 0 make the calc or show a error message if they are 0. Although it doesnt the show error message when hours are 0
Public Class frmGross
Private Sub btnCalcuate_Click(sender As Object, e As EventArgs) Handles btnCalcuate.Click
Dim decHours As Decimal
Dim decHourlyWage As Decimal
Dim decTotalPay As Decimal
decHours = txtHours.Text
decHourlyWage = txtHourlyWage.Text
decTotalPay = decHours * decHourlyWage
LblTotalPay.Text = decTotalPay
If decHours >= 0 Then
decHours = txtHours.Text
decHourlyWage = txtHourlyWage.Text
decTotalPay = decHours * decHourlyWage
LblTotalPay.Text = decTotalPay.ToString("C2")
Else
LblTotalPay.Text = "Error"
End If
End Sub
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Me.Close()
End SubEnd Class
4
u/CityEggs VB.Net Beginner Nov 02 '18
Here is another hint: decHourlyWage is a number, txtHourlyWage.Text is a string; Some conversion may need to be done if you want to do maths with it
2
u/andrewsmd87 Web Specialist Nov 02 '18
A few things. Before you assign decHours = txtHours.txt you need to use the IsNumeric function on it. I.e. type in asdf and click your button, you'll get an error. You need to do something along the lines of if not isnumeric(txtHours.Text) Then 'show an error message
Next, it's not going to show an error on 0 because >=, as someone else mentioned, means greater than or equal to. So 0 is equal to 0, thus it's true.
5
u/[deleted] Nov 02 '18
Here's a hint: ">=" means "greater than or equal to".