[RESOLVED] Help incrementing a variable please.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Help incrementing a variable please.

  1. #1
    Join Date
    Oct 2013
    Posts
    2

    [RESOLVED] Help incrementing a variable please.

    I hope I'm posting in the correct forum.

    I have the variable dblCalories += 19.5 which I am using in a For Next Loop. I need to have dblCalories start out at the number 39...NOT 19.5, and then add 19.5 to 39 and so on. Make sense I hope? Here is my code. IF you run it, you will see that it is showing 19.5 in the first output but what I need it to start out showing is 39...and then go up by 19 from there up. Thanks for any help.

    Code:
    Public Class Form1
    
        Private Sub btnRunDemo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRunDemo.Click
            Dim intCount As Integer     ' Loop counter
            Dim dblCalories As Double  ' To hold calories
            Dim strTemp As String       ' To hold output
    
            ' Initiate count at 5 and Step 5
            For intCount = 5 To 30 Step 5
    
                ' Calculate the calories of dblCalories.
                dblCalories += 19.5
    
                ' Create a string to display.
                strTemp = "The number of calories burned for " & intCount.ToString() & " minutes" &
                    " is " & dblCalories.ToString()
    
                ' Add the string to the list box.
                lstOutput.Items.Add(strTemp)
            Next
        End Sub
    
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
            ' Clear the list box.
            lstOutput.Items.Clear()
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            ' Close the form.
            Me.Close()
        End Sub
    End Class

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,844

    Re: Help incrementing a variable please.

    Well you could simply initialize the variable before the loop
    Code:
    Dim dblCalories As Double  ' To hold calories
    Change the above to
    Code:
    Dim dblCalories As Double  =39 ' To hold calories
    You may also want to move the
    Code:
    dblCalories += 19.5
    down a few lines so it adds the 19.5 at the bottom of the loop rather than at the top

    or you could initialize the variable at a lower number and leave the code in the loop as is
    Code:
    Dim dblCalories As Double  =20.5 ' To hold calories
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Oct 2013
    Posts
    2

    Re: Help incrementing a variable please.

    Quote Originally Posted by DataMiser View Post
    Well you could simply initialize the variable before the loop
    Code:
    Dim dblCalories As Double  ' To hold calories
    Change the above to
    Code:
    Dim dblCalories As Double  =39 ' To hold calories
    You may also want to move the
    Code:
    dblCalories += 19.5
    down a few lines so it adds the 19.5 at the bottom of the loop rather than at the top

    or you could initialize the variable at a lower number and leave the code in the loop as is
    Code:
    Dim dblCalories As Double  =20.5 ' To hold calories
    Thank you! I had tried Dim dblCalories As Double =39, but was having issues with 19.5 being added to that as well. Your advice about moving dblCalories lower in the code did the trick! I didn't know that simply moving a variable to a different location with the loop would make a difference but it does! Lesson learned!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center