Hello,

I am a newbie and I am learning Visual Basic 2008. My homework is the following:
ublic Class Form1


' This Internet Service provider offers three subscription packages
' to its customers, plus a 20% discount in all packages
' for nonprofit organizations.

' Package A: 10 hours of access for: $ 9.95 per month;
' Additional hours are: $ 2.00 per hour.

' Package B: 20 hours of access for: $ 14.95 per month;
' Additional hours are: $ 1.00 per hour.

'Package C: Unlimited access for: $ 19.95 per month.

' The following class-level constant is used
' to calculate discount.

Const decDiscountRate As Decimal = 0.8D

I created the radio buttons for each package ( they work), and the user has to enter the number of hours using a textbox ( it works after I changed the hours from integer to double).

The display label for TotalAmountDue it works too.

The checkbox captioned ""NonProfit Organization" , it works too.
However, my IF/Then..Else IF statements does not work. The only time it works is when I check package A; then display the $9.95 due.

In addition, I have to use a textbox's validating event to do the validation that hours used in a month cannot exceed 744 (but I will take care of that later).

Could you please tell me, why my loop does not work?

Thank you

Private Sub btnOkay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkay.Click
' Declare variable
Dim strMessage As String = String.Empty

'The following If..ElseIf..statements test the
' group of radio buttons and copies the first part
' of the message to strMessage.

If radPackageA.Checked = True Then
MessageBox.Show("You selected Package A")
ElseIf radPackageB.Checked = True Then
MessageBox.Show("You selected Package B")
ElseIf radPackageC.Checked = True Then
MessageBox.Show("You selected Package C")
End If

' The following If...Then satements the check box.

If chkNonProfit.Checked = True Then
strMessage = "Non Profit organization"
End If

' Now display the message
MessageBox.Show(strMessage)

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

' Clear the form, reset the buttons and CheckBox.

radPackageA.Checked = True
radPackageB.Checked = True
radPackageC.Checked = True
txtHoursUsed.Clear()
lblNumberofHours.Text = String.Empty
lblTotalAmountDue.Text = String.Empty

' Return the focus to txtHoursUsed
txtHoursUsed.Focus()

End Sub

Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click

' End the application by closing the window.
Me.Close()

End Sub

Private Sub btnCalcAmountDue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcAmountDue.Click

' Declare local variables.

Dim dblHoursUsed As Double ' to hold hours used
Dim dblAmountDue As Double

' Read the value from the TextBox Control.

dblAmountDue = CDbl(txtHoursUsed.Text)

'Calculate the amount due and the 20% discount for (nonprofit)
' using the If/ElseIf statement.

If radPackageA.Checked = True Then
dblAmountDue = 9.95

If (dblHoursUsed > 10) Then
dblAmountDue = 9.95 + (dblHoursUsed - 10) * 2.0
End If

lblTotalAmountDue.Text = CStr(dblAmountDue)

ElseIf radPackageB.Checked = True Then

dblAmountDue = 14.95

ElseIf (dblHoursUsed > 20) Then

dblAmountDue = 14.95 + (dblHoursUsed - 20) * 1.0

If radPackageC.Checked = True Then
dblAmountDue = 19.95

Else(nonprofit) Then
dblAmountDue = dblAmountDue * decDiscountRate

End If

' Display the amount due.
lblTotalAmountDue.Text = CStr(dblAmountDue)

End If

End Sub

Private Sub txtErrorHoursValidation_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtErrorHoursValidation.TextChanged

' Validate the number entered by the user.

If IsNumeric(txtErrorHoursValidation.Text) Then
Dim intHoursUsed As Integer
intHoursUsed = CInt(txtErrorHoursValidation.Text)

If intHoursUsed > 744 Then
lblMessage.BackColor = Color.Red
lblMessage.Text = " Incorrect Data"

End If

End If

End Sub

Private Sub txtErrorHoursValidation_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtErrorHoursValidation.Validating


End Sub
End Class

If radPackageA.Checked = True Then
        dblAmountDue = 9.95

        If (dblHoursUsed > 10) Then
                dblAmountDue = 9.95 + (dblHoursUsed - 10) * 2.0
        End If
        lblTotalAmountDue.Text = CStr(dblAmountDue)

ElseIf radPackageB.Checked = True Then
        dblAmountDue = 14.95
ElseIf (dblHoursUsed > 20) Then
        dblAmountDue = 14.95 + (dblHoursUsed - 20) * 1.0

        If radPackageC.Checked = True Then
                dblAmountDue = 19.95
        Else(nonprofit) Then
                dblAmountDue = dblAmountDue * decDiscountRate
        End If

        ' Display the amount due.
        lblTotalAmountDue.Text = CStr(dblAmountDue)
End If

The only spot your adding any info back to a label is in the first part of that If statement where radPackageA.Checked = True or if A & B are both not check and Hours are greate then 20

Thank you but I knew that ( this is my first project).My problem is that I don't know, what I am doing wrong. Now, I got another problem when I run the program, I get the following message : ' Read the value from the TextBox Control.

dblAmountDue = CDbl(txtHoursUsed.Text)
" Invalid cast exception"

That would be caused by a value within the textbox unable to be converted. I have two suggestions for this, the first which is the better solution may not be allowed by your class assignment and that is to use a NumericUpDown control instead of a textbox. It works the same as a textbox but only allows users to enter numbers and it returns a decimal value. My second suggestion for this would be to use Decimal.TryParse or Double.TryParse, both have examples in the help file, if you still need help with that let me know. (I would suggest using decimal over double for currency values)

Im at work now myself so it is hard for me to go line by line & figure out what your coding is doing.

I fixed!! I initialized the Dim dblHoursUsed As Double = 0 ' to hold hours used. Now, I need to make my If statements works and the nonprofit.

***************************

the If statements only work for the default values. But, then I made some changes to the IF statements, run the program again, and I got the following error message: " Conversion from String to type double is not valid"

Why it works the first time and not the second time? Here is the changes I made to the code:

Dim dblHoursUsed As Double ' to hold hours used
Dim decAmountDue As Decimal
decAmountDue = CDec(CDbl(txtHoursUsed.Text))

' Check the number of hours used and exit if it contains
' invalid data.
If Decimal.TryParse(txtHoursUsed.Text, CDec(dblHoursUsed)) Then
lblNumberofHours.Text = "Success!"

End If

thanks again

Forget it!! It works only the first two runs, then got the same error message again. pls. help!!

I have the exact same project lol.... and im having the same problem and i think your in my class.... lol ..

This is funny and sad. I am so overwhelmed with homework , midterms and projects that I cannot spend my time spinning my wheels like I did with the other programs.

Seriously, I get the error at runtime at the third try. I will try to fixed it tomorrow.

Good luck!!

If radPackageA.Checked = True Then        
dblAmountDue = 9.95         
If (dblHoursUsed > 10) Then                
dblAmountDue = 9.95 + (dblHoursUsed - 10) * 2.0        
End If        
lblTotalAmountDue.Text = CStr(dblAmountDue) 
ElseIf radPackageB.Checked = True Then        
dblAmountDue = 14.95ElseIf (dblHoursUsed > 20) Then        
dblAmountDue = 14.95 + (dblHoursUsed - 20) * 1.0         
If radPackageC.Checked = True Then                
dblAmountDue = 19.95       
 
[B]IfElse[B](B] Then               
 dblAmountDue = dblAmountDue * decDiscountRate        End If[/B]

Create a Const decDiscountRate = .8D
Correct the bolded
Correct The bold areas

Thank You! I am done with this program and I will start working in Part 2
(Shipping company) tomorrow.

Thanks for all the help

Seems your are already done with your project. Sorry for the late response, Ive been unable to get back in a timely manner.

Just for your understanding I wanted to point out a few mistakess. First you dont need a double (pun) conversion; you dont need to convert first to a double and then to a decimal. Converting straight to the datatype you want is fine. Second with the Decimal.TryParse method you dont need to convert (cdec()) to a decimal, it will automatically do that for you.

The TryParse has two purposes, first it will automatically convert your value into the variable you provide and second it will return a true/false answer whether it was successfull or not. Lastly you wouldnt want to use Decimal.TryParse to store a value into a datatype of double, in that case either change the variable datatype to a decimal or use a Double.TryParse.

Dim dblHoursUsed As Double     ' to hold hours used
        Dim decAmountDue As Decimal
        decAmountDue = CDec(CDbl(txtHoursUsed.Text))

        ' Check the number of hours used and exit if it contains
        ' invalid data.
      If Decimal.TryParse(txtHoursUsed.Text, CDec(dblHoursUsed)) Then
            lblNumberofHours.Text = "Success!"

        End If

Thank you, Tom. I have three more programs to write. I am reading about class and objects. I used and object (Me.) and TextBox1 as property for the first assignment. Now, I am working in program 2 of the assignment.

Thanks again

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.