7 Replies - 2367 Views - Last Post: 04 November 2011 - 04:40 PM Rate Topic: -----

#1 cartoonmeister  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-November 11

VB 2010 Distance Calculator Loop Issue

Posted 04 November 2011 - 10:33 AM

Does anyone know what I'm doing wrong? I can't get the loop to work.
The calculator is suppose to show how far someone goes for each hour increment (i.e. if they punch in 3 hours, it should show a number for 1 hour, 2 hours and 3 hours; if they punch in 10 hours it should show a number for 1 hour, 2 hours, 3 hours....etc. up to ten hours, etc.) I can't get it to show anything except for the total number of hours traveled and total distance. : ( Any ideas?

P
rivate Sub DistanceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DistanceButton.Click
        Dim speedIsValid As Boolean
        Dim hoursIsValid As Boolean
        Dim distanceIsValid As Boolean

        Dim speed As Double
        Dim hours As Integer

        Dim distance As Double

        speedIsValid = Double.TryParse(SpeedTextBox.Text, speed)
        hoursIsValid = Integer.TryParse(HoursTextBox.Text, hours)
        distanceIsValid = Double.TryParse(DistanceTextBox.Text, distance)

        If speed >= 1 And hours >= 1 Then
            DistanceTextBox.Text = "Do not use this box."
        End If

        'DistanceTextBox.Text = String.Empty

        Do
            For hours = 1 To 24
            Next hours
            ResultsTextBox.Text = hours.ToString

        Loop


        ResultsTextBox.Text = ("Vehicle Speed:") + (" ") + SpeedTextBox.Text +        (" ") + ("MPH") + ("                 ") + ("Time Traveled:") + (" ") + HoursTextBox.Text + (" ") + ("hours") + ("                     ") + ("-------------------------------------------") + ("Total Distance Traveled:") + (" ") + (speed * hours).ToString + (" ") + ("Miles")


        'Add loop business after ------ on line 64 and pads
        'do like video Ch7 For Nextk


    End Sub

This post has been edited by AdamSpeight2008: 04 November 2011 - 11:36 AM
Reason for edit:: Please remember the codetags


Is This A Good Question/Topic? 0
  • +

Replies To: VB 2010 Distance Calculator Loop Issue

#2 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: VB 2010 Distance Calculator Loop Issue

Posted 04 November 2011 - 10:45 AM

Well, you're overwriting the text in ResultsTextBox on every iteration of the loop, and your loop is funky anyway. And the piece of code that I believe is actually showing all your data is completely outside of your loop. I'd try something along the lines of this & see where that gets you:
...

For i = 1 To hours

  ResultsTextBox.Text = ("Vehicle Speed:") + (" ") + SpeedTextBox.Text + (" ") + ("MPH") + (" ") + ("Time Traveled:") + (" ") + HoursTextBox.Text + (" ") + ("hours") + (" ") + ("-------------------------------------------") + ("Total Distance Traveled:") + (" ") + (speed * i).ToString + (" ") + ("Miles") & Environment.NewLine

Next

Was This Post Helpful? 0
  • +
  • -

#3 cartoonmeister  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-November 11

Re: VB 2010 Distance Calculator Loop Issue

Posted 04 November 2011 - 01:45 PM

What is "i" ???
Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: VB 2010 Distance Calculator Loop Issue

Posted 04 November 2011 - 01:49 PM

i is the loop counter variable. Originally you had hours as your loop counter variable, which overwrote anything the user entered for hours. I assumed you didn't want that, but instead wanted your loop to execute a number of times corresponding to the amount of hours entered. Therefore, you need to create a new variable (in this case, i) to use to control your loop, and have it go from 1 to the number of hours entered.
Was This Post Helpful? 1
  • +
  • -

#5 DimitriV  Icon User is offline

  • Das kann nicht sein
  • member icon

Reputation: 548
  • View blog
  • Posts: 2,643
  • Joined: 24-July 11

Re: VB 2010 Distance Calculator Loop Issue

Posted 04 November 2011 - 03:32 PM

Wouldnt you actually say: For i As Integer = suchandsuch?
J-Bo
Was This Post Helpful? 1
  • +
  • -

#6 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: VB 2010 Distance Calculator Loop Issue

Posted 04 November 2011 - 03:37 PM

View PostjimmyBo, on 04 November 2011 - 04:32 PM, said:

Wouldnt you actually say: For i As Integer = suchandsuch?
J-Bo

Yep, you would.
Copy/paste/modify: 1
Me: 0
Was This Post Helpful? 1
  • +
  • -

#7 DimitriV  Icon User is offline

  • Das kann nicht sein
  • member icon

Reputation: 548
  • View blog
  • Posts: 2,643
  • Joined: 24-July 11

Re: VB 2010 Distance Calculator Loop Issue

Posted 04 November 2011 - 04:04 PM

Wouldn't that code be overwriting anyway? You've added ResultsBox.Text =. Shouldn't it be ResultsBox.Text += such & such each time the loop runs?
Was This Post Helpful? 1
  • +
  • -

#8 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: VB 2010 Distance Calculator Loop Issue

Posted 04 November 2011 - 04:40 PM

View PostjimmyBo, on 04 November 2011 - 05:04 PM, said:

Wouldn't that code be overwriting anyway? You've added ResultsBox.Text =. Shouldn't it be ResultsBox.Text += such & such each time the loop runs?

Yep. Sigh, must be a Friday. Good eye.

Copy/paste/modify: 2
Me: 0
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1