11 Replies - 13328 Views - Last Post: 12 November 2010 - 06:45 AM Rate Topic: -----

#1 SusieQ'n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-November 10

Do While Loop - Visual Basic 2010

Posted 11 November 2010 - 03:09 PM

Hi I'm working on a project in Visual Basic 2010 and I'm really new to this code. I have everything working except a Do While Loop that is supposed to be added into the project. I don't totally understand the mechanics of the Do While Loop, I realize that it will repeat until the condition evaluates to False. But when actually implementing it into my program I don't understand how it is supposed to run. Here is what I need to accomplish:
"Use radio buttons for the customer type and have them default to both being unselected when the program starts. Include a DO WHILE loop that checks to make sure one customer type is selected when clicking on the button and then automatically selects one or the other before ending the loop."

This is my code:
Public Class Form1

    Private Sub btnCalculateBill_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculateBill.Click

        'Declare Variables

        Dim bgnOdo As String = Me.txtBeginOdometer.Text
        Dim endOdo As String = Me.txtEndOdometer.Text
        Dim numDayRent As String = Me.txtNumDayRent.Text
        Dim numOfMiles As Decimal = 0
        Const mileChrgB As Decimal = 0.12       '$0.12 for business
        Const mileChrgP As Decimal = 0.1        '$0.10 for personal
        Const rentalChrgB As Decimal = 15.0     '$15.00 for business
        Const rentalChrgP As Decimal = 12.5     '$12.50 for personal
        Dim totMileChrg As Decimal = 0
        Dim totRentalChrg As Decimal = 0
        Dim totDue As Decimal = 0

        numOfMiles = endOdo - bgnOdo

        Do While Me.radPersonal.Checked = True
            Me.radBusiness.Checked = True
            Me.radPersonal.Checked = False
        Loop


        If Me.radBusiness.Checked Then                    'Business Customer
            totMileChrg = mileChrgB * numOfMiles
            totRentalChrg = numDayRent * rentalChrgB
            totDue = totMileChrg + totRentalChrg
        End If

        If Me.radPersonal.Checked Then                     'Personal Customer
            totMileChrg = mileChrgP * numOfMiles
            totRentalChrg = numDayRent * rentalChrgP
            totDue = totMileChrg + totRentalChrg
        End If

        Me.lblAmtOfMiles.Text = Format(numOfMiles, "General Number")
        Me.lblAmtOfMileChrg.Text = Format(totMileChrg, "Currency")
        Me.lblAmtDayUseChrg.Text = Format(totRentalChrg, "Currency")
        Me.lblTotDueDisplay.Text = Format(totDue, "Currency")

    End Sub



I have to radio buttons, one for Personal customers and one for Business Customers, each having there own price amount. When I run my program with the Do While Loop I have now, it lets me pick between personal or business, but when I click "Calculate Customer Bill" it automatically picks Business every time. I don't understand how to make the loop do as the instructions request "then automatically selects one or the other before ending the loop". Any advice or a better explanation on How a DO While Loop works would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Do While Loop - Visual Basic 2010

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Do While Loop - Visual Basic 2010

Posted 11 November 2010 - 04:26 PM

Do While Me.radPersonal.Checked = True
            Me.radBusiness.Checked = True
            Me.radPersonal.Checked = False
        Loop


Let's break this down.
If you click business then the Do While isn't triggered, why? because you only DO while Personal is checked.

So when you click personal, the loop is triggered and you immediately set business to true and personal to false.

The Do While loop you have is unnecessary in the scenerio you explained. Remark it out and try your code, see what result you get then.

This post has been edited by CharlieMay: 12 November 2010 - 05:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 PeterH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 03-September 09

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 03:17 AM

View PostSusieQ, on 11 November 2010 - 02:09 PM, said:

Hi I'm working on a project in Visual Basic 2010 and I'm really new to this code. I have everything working except a Do While Loop that is supposed to be added into the project. I don't totally understand the mechanics of the Do While Loop, I realize that it will repeat until the condition evaluates to False. But when actually implementing it into my program I don't understand how it is supposed to run. Here is what I need to accomplish:
"Use radio buttons for the customer type and have them default to both being unselected when the program starts. Include a DO WHILE loop that checks to make sure one customer type is selected when clicking on the button and then automatically selects one or the other before ending the loop."

This is my code:
Public Class Form1

    Private Sub btnCalculateBill_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculateBill.Click

        'Declare Variables

        Dim bgnOdo As String = Me.txtBeginOdometer.Text
        Dim endOdo As String = Me.txtEndOdometer.Text
        Dim numDayRent As String = Me.txtNumDayRent.Text
        Dim numOfMiles As Decimal = 0
        Const mileChrgB As Decimal = 0.12       '$0.12 for business
        Const mileChrgP As Decimal = 0.1        '$0.10 for personal
        Const rentalChrgB As Decimal = 15.0     '$15.00 for business
        Const rentalChrgP As Decimal = 12.5     '$12.50 for personal
        Dim totMileChrg As Decimal = 0
        Dim totRentalChrg As Decimal = 0
        Dim totDue As Decimal = 0

        numOfMiles = endOdo - bgnOdo

        Do While Me.radPersonal.Checked = True
            Me.radBusiness.Checked = True
            Me.radPersonal.Checked = False
        Loop


        If Me.radBusiness.Checked Then                    'Business Customer
            totMileChrg = mileChrgB * numOfMiles
            totRentalChrg = numDayRent * rentalChrgB
            totDue = totMileChrg + totRentalChrg
        End If

        If Me.radPersonal.Checked Then                     'Personal Customer
            totMileChrg = mileChrgP * numOfMiles
            totRentalChrg = numDayRent * rentalChrgP
            totDue = totMileChrg + totRentalChrg
        End If

        Me.lblAmtOfMiles.Text = Format(numOfMiles, "General Number")
        Me.lblAmtOfMileChrg.Text = Format(totMileChrg, "Currency")
        Me.lblAmtDayUseChrg.Text = Format(totRentalChrg, "Currency")
        Me.lblTotDueDisplay.Text = Format(totDue, "Currency")

    End Sub



I have to radio buttons, one for Personal customers and one for Business Customers, each having there own price amount. When I run my program with the Do While Loop I have now, it lets me pick between personal or business, but when I click "Calculate Customer Bill" it automatically picks Business every time. I don't understand how to make the loop do as the instructions request "then automatically selects one or the other before ending the loop". Any advice or a better explanation on How a DO While Loop works would be appreciated.


I agree with the first reply, but in order to implement the requirment 'as stated' in your text I would add a new boolean variable and perform the DoWhile loop until this variable is true, then within the loop, test for either rdo.button being selected, if neither display a message box inviting a selection, then once a selection is made, set variable to true end the loop. Not pretty tho.
Was This Post Helpful? 0
  • +
  • -

#4 SusieQ'n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-November 10

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 05:29 AM

View PostCharlieMay, on 11 November 2010 - 03:26 PM, said:

Do While Me.radPersonal.Checked = True
            Me.radBusiness.Checked = True
            Me.radPersonal.Checked = False
        Loop


Let's break this down.
If you click business then the Do While isn't triggered, why? because you only DO while Personal is checked.

So when you click personal, the loop is triggered and you immediately set business to true and personal to false.

The Do While loop you have is unnecessary in the scenerio you explained. Remark it out and try your code, see what result you get then.



Thank you CharlieMay for explaining what is occurring in my current loop. Makes sense, and I agree it is not needed in the code. Unfortunately the outline of my project requires me to include it. I appreciate the reply. =)

This post has been edited by SusieQ'n: 12 November 2010 - 05:30 AM

Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 05:31 AM

Sorry, I must have missed the text above the code box.

Maybe something like this would fulfill the requirement of the Do While Loop

        Do While radBusiness.Checked = False And radPersonal.Checked = False
            MessageBox.Show("No category was selected, defaulting to business")
            radBusiness.Checked = True
        Loop

The messagebox isn't required, I would personally notify the user prior to setting something a default. It can be removed if you haven't went through them in the course yet.

Do While radBusiness.Checked = False and radPersonal.Checked = False ' This checks to make sure at least one is checked. Since we know if both are false then none are checked the Do While is performing its function in this case.

If it finds that the user didn't check one, it sets business to checked and continues on through the rest of the code.

Of course if one or the other is checked, the do while ends immediately and continues on through the rest of the code.
Was This Post Helpful? 1
  • +
  • -

#6 SusieQ'n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-November 10

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 05:33 AM

View PostPeterH, on 12 November 2010 - 02:17 AM, said:

View PostSusieQ, on 11 November 2010 - 02:09 PM, said:

Hi I'm working on a project in Visual Basic 2010 and I'm really new to this code. I have everything working except a Do While Loop that is supposed to be added into the project. I don't totally understand the mechanics of the Do While Loop, I realize that it will repeat until the condition evaluates to False. But when actually implementing it into my program I don't understand how it is supposed to run. Here is what I need to accomplish:
"Use radio buttons for the customer type and have them default to both being unselected when the program starts. Include a DO WHILE loop that checks to make sure one customer type is selected when clicking on the button and then automatically selects one or the other before ending the loop."

This is my code:
Public Class Form1

    Private Sub btnCalculateBill_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculateBill.Click

        'Declare Variables

        Dim bgnOdo As String = Me.txtBeginOdometer.Text
        Dim endOdo As String = Me.txtEndOdometer.Text
        Dim numDayRent As String = Me.txtNumDayRent.Text
        Dim numOfMiles As Decimal = 0
        Const mileChrgB As Decimal = 0.12       '$0.12 for business
        Const mileChrgP As Decimal = 0.1        '$0.10 for personal
        Const rentalChrgB As Decimal = 15.0     '$15.00 for business
        Const rentalChrgP As Decimal = 12.5     '$12.50 for personal
        Dim totMileChrg As Decimal = 0
        Dim totRentalChrg As Decimal = 0
        Dim totDue As Decimal = 0

        numOfMiles = endOdo - bgnOdo

        Do While Me.radPersonal.Checked = True
            Me.radBusiness.Checked = True
            Me.radPersonal.Checked = False
        Loop


        If Me.radBusiness.Checked Then                    'Business Customer
            totMileChrg = mileChrgB * numOfMiles
            totRentalChrg = numDayRent * rentalChrgB
            totDue = totMileChrg + totRentalChrg
        End If

        If Me.radPersonal.Checked Then                     'Personal Customer
            totMileChrg = mileChrgP * numOfMiles
            totRentalChrg = numDayRent * rentalChrgP
            totDue = totMileChrg + totRentalChrg
        End If

        Me.lblAmtOfMiles.Text = Format(numOfMiles, "General Number")
        Me.lblAmtOfMileChrg.Text = Format(totMileChrg, "Currency")
        Me.lblAmtDayUseChrg.Text = Format(totRentalChrg, "Currency")
        Me.lblTotDueDisplay.Text = Format(totDue, "Currency")

    End Sub



I have to radio buttons, one for Personal customers and one for Business Customers, each having there own price amount. When I run my program with the Do While Loop I have now, it lets me pick between personal or business, but when I click "Calculate Customer Bill" it automatically picks Business every time. I don't understand how to make the loop do as the instructions request "then automatically selects one or the other before ending the loop". Any advice or a better explanation on How a DO While Loop works would be appreciated.


I agree with the first reply, but in order to implement the requirment 'as stated' in your text I would add a new boolean variable and perform the DoWhile loop until this variable is true, then within the loop, test for either rdo.button being selected, if neither display a message box inviting a selection, then once a selection is made, set variable to true end the loop. Not pretty tho.


Thanks PeterH, I will try a new variable, see what I can make of it. I agree it wont be pretty.
Was This Post Helpful? 0
  • +
  • -

#7 SusieQ'n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-November 10

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 05:53 AM

View PostCharlieMay, on 12 November 2010 - 04:31 AM, said:

Sorry, I must have missed the text above the code box.

Maybe something like this would fulfill the requirement of the Do While Loop

        Do While radBusiness.Checked = False And radPersonal.Checked = False
            MessageBox.Show("No category was selected, defaulting to business")
            radBusiness.Checked = True
        Loop

The messagebox isn't required, I would personally notify the user prior to setting something a default. It can be removed if you haven't went through them in the course yet.

Do While radBusiness.Checked = False and radPersonal.Checked = False ' This checks to make sure at least one is checked. Since we know if both are false then none are checked the Do While is performing its function in this case.

If it finds that the user didn't check one, it sets business to checked and continues on through the rest of the code.

Of course if one or the other is checked, the do while ends immediately and continues on through the rest of the code.


Ok, what you stated makes sense and I thought the same thing. I tried Do While radBusiness.Checked = False And radPersonal.Checked = False, and I like the message box, we have covered them, so I can leave it in. The problem is the third line radBusiness.Checked = True, locks up the program. I have tried radBusiness.Checked = False, it also locked up the program. It just freezes, I have to do Ctrl, Alt, Delete to get out of it. I'm going to continue to tweak it and see what happens. Truly awesome that you helped. I feel better knowing that some of the things I tried weren't complete out in left field. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#8 SusieQ'n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-November 10

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 06:16 AM

Quote

Sorry, I must have missed the text above the code box.

Maybe something like this would fulfill the requirement of the Do While Loop

        Do While radBusiness.Checked = False And radPersonal.Checked = False
            MessageBox.Show("No category was selected, defaulting to business")
            radBusiness.Checked = True
        Loop

The messagebox isn't required, I would personally notify the user prior to setting something a default. It can be removed if you haven't went through them in the course yet.

Do While radBusiness.Checked = False and radPersonal.Checked = False ' This checks to make sure at least one is checked. Since we know if both are false then none are checked the Do While is performing its function in this case.

If it finds that the user didn't check one, it sets business to checked and continues on through the rest of the code.

Of course if one or the other is checked, the do while ends immediately and continues on through the rest of the code.


Quote

Ok, what you stated makes sense and I thought the same thing. But I placed Or instead of And. Your way worked, and my Or did not,lol. So now it works!!!!!!! Thank you, thank you!!!! Woot! Feel so much better, yah!

This post has been edited by SusieQ'n: 12 November 2010 - 06:16 AM

Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 06:21 AM

Do you have any code in the CheckChanged Event of either of the radbuttons? It might be triggering an endless loop. If not, re-post what you have now.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_SusieQ'n*


Reputation:

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 06:34 AM

View PostCharlieMay, on 12 November 2010 - 05:21 AM, said:

Do you have any code in the CheckChanged Event of either of the radbuttons? It might be triggering an endless loop. If not, re-post what you have now.

Was This Post Helpful? 0

#11 SusieQ'n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-November 10

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 06:42 AM

View PostSusieQ, on 12 November 2010 - 05:34 AM, said:

View PostCharlieMay, on 12 November 2010 - 05:21 AM, said:

Do you have any code in the CheckChanged Event of either of the radbuttons? It might be triggering an endless loop. If not, re-post what you have now.


Sorry for the confusion, your way does work. I just accidently put Or instead of the And u stated. It works and I truly appreciate your help, you rock!
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Do While Loop - Visual Basic 2010

Posted 12 November 2010 - 06:45 AM

Yea, I caught that after I posted, glad it's working :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1