Results 1 to 2 of 2

Thread: Loop Issue

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    14

    Loop Issue

    Good Evening everyone,

    I'm having a bit of an issue with my code. I know what the issue is but I'm not sure the best way of going around it.

    It's a school project and I'm not allowed to use Return, Throw etc in it.


    I was trying to use a do while loop to validation and force a user to re-enter correct data. I get all the correct error boxes and messages, but the loop keeps forcing the message boxes before the error can be corrected.

    Im sure I could change it around and just use IF Statements but is that the best way ?

    Here is the main code

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    Const dblTenCents As Double = 0.1
    Const dblThirteenCents As Double = 0.13
    Dim intMin As Integer
    Dim intMax As Integer
    Dim MinTotalPrice As Double
    Dim MaxTotalPrice As Double
    Dim blnGood As Boolean
    blnGood = False

    Try



    Do

    lblTotalCalc.Text = String.Empty

    If cboTitle.Text = "" Then
    MessageBox.Show("Please Enter Title, Example Mr. \ Mrs.")
    lblTotalCalc.Text = String.Empty
    'Return
    'End If

    ElseIf txtFirstName.Text = "" Then
    MessageBox.Show("Please Enter First Name")
    lblTotalCalc.Text = String.Empty
    'Return
    'End If

    ElseIf txtLastName.Text = "" Then
    MessageBox.Show("Please Enter Last Name")
    lblTotalCalc.Text = String.Empty
    'Return
    'End If

    ElseIf Not Integer.TryParse(cboMin.Text, intMin) Or String.IsNullOrWhiteSpace(cboMin.Text) Then
    MessageBox.Show("Please Enter or select a Whole Number Greater than 0")
    lblTotalCalc.Text = String.Empty
    'Return

    ElseIf intMin < 0 Then
    MessageBox.Show("Please Enter or select a Whole Number Greater than 0 for Min")
    lblTotalCalc.Text = String.Empty
    'Return

    ElseIf intMin > 300 Then
    MessageBox.Show("Minimum Value cannot be greater than 300 , Please Re-Enter")
    lblTotalCalc.Text = String.Empty
    'Return

    'Making sure that there is a valid number
    ElseIf Not Integer.TryParse(cboMax.Text, intMax) Or String.IsNullOrWhiteSpace(cboMax.Text) Then
    MessageBox.Show("Please Enter or select a whole Number Greater than 0 for Max")
    lblTotalCalc.Text = String.Empty
    'Return

    'Making sure that the number is not greater than 300
    ElseIf intMax > 300 Then
    MessageBox.Show("Maximum Valuse cannot be greater than 300, Please Re-Enter")
    lblTotalCalc.Text = String.Empty
    'Return

    'Making sure that the Minimun is not greater than the Max.
    ElseIf intMin > intMax Then
    MessageBox.Show("Mininum Number cannot be greater than the Maximum, Please Re-Enter")
    lblTotalCalc.Text = String.Empty
    'Return

    Else
    blnGood = True

    End If
    'Program will loop until the data is true and flip the flag to true for calculations
    Loop Until blnGood = True


    Catch ex As Exception

    End Try

  2. #2

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    14

    Re: Loop Issue

    I changed it to this and it's better now. If there was a way to make the loop work please let me know.


    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    Const dblTenCents As Double = 0.1
    Const dblThirteenCents As Double = 0.13
    Dim intMin As Integer
    Dim intMax As Integer
    Dim MinTotalPrice As Double
    Dim MaxTotalPrice As Double
    Dim blnGood As Boolean
    blnGood = False

    Try

    'Do

    lblTotalCalc.Text = String.Empty

    If cboTitle.Text = "" Then
    MessageBox.Show("Please Enter Title, Example Mr. \ Mrs.")
    lblTotalCalc.Text = String.Empty
    'Return
    'End If

    ElseIf txtFirstName.Text = "" Then
    MessageBox.Show("Please Enter First Name")
    lblTotalCalc.Text = String.Empty
    'Return
    'End If

    ElseIf txtLastName.Text = "" Then
    MessageBox.Show("Please Enter Last Name")
    lblTotalCalc.Text = String.Empty
    'Return
    'End If

    ElseIf Not Integer.TryParse(cboMin.Text, intMin) Or String.IsNullOrWhiteSpace(cboMin.Text) Then
    MessageBox.Show("Please Enter or select a Whole Number Greater than 0")
    lblTotalCalc.Text = String.Empty
    'Return

    ElseIf intMin < 0 Then
    MessageBox.Show("Please Enter or select a Whole Number Greater than 0 for Min")
    lblTotalCalc.Text = String.Empty
    'Return

    ElseIf intMin > 300 Then
    MessageBox.Show("Minimum Value cannot be greater than 300 , Please Re-Enter")
    lblTotalCalc.Text = String.Empty
    'Return

    'Making sure that there is a valid number
    ElseIf Not Integer.TryParse(cboMax.Text, intMax) Or String.IsNullOrWhiteSpace(cboMax.Text) Then
    MessageBox.Show("Please Enter or select a whole Number Greater than 0 for Max")
    lblTotalCalc.Text = String.Empty
    'Return

    'Making sure that the number is not greater than 300
    ElseIf intMax > 300 Then
    MessageBox.Show("Maximum Valuse cannot be greater than 300, Please Re-Enter")
    lblTotalCalc.Text = String.Empty
    'Return

    'Making sure that the Minimun is not greater than the Max.
    ElseIf intMin > intMax Then
    MessageBox.Show("Mininum Number cannot be greater than the Maximum, Please Re-Enter")
    lblTotalCalc.Text = String.Empty
    'Return

    Else
    ' blnGood = True

    If intMin > 20 Then
    MinTotalPrice = (dblThirteenCents * 20) + ((intMin - 20) * dblTenCents)

    Else
    MinTotalPrice = (dblThirteenCents * intMin)

    End If

    If intMax > 20 Then
    MaxTotalPrice = (dblThirteenCents * 20) + ((intMax - 20) * dblTenCents)
    Else
    MaxTotalPrice = (dblThirteenCents * intMax)

    End If

    lblTotalCalc.Text = cboTitle.Text & " " & txtLastName.Text & " The Total Cost for the Minimum Minutes is " & MinTotalPrice.ToString("C") & Environment.NewLine & " and the Total Cost for Maximun Minutes is " & MaxTotalPrice.ToString("C")

    End If
    'Program will loop until the data is true and flip the flag to true for calculations
    'Loop Until blnGood = True


    Catch ex As Exception

    End Try

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width