CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2011
    Posts
    1

    HELP with For Next...

    Okay, so I have to create program that adds the whole numbers from 1 to 1000 and displays the sum in the label. Create a variation of this program that allows the user to specify any starting whole number and any ending whole number (10,000 or lower) and then adds all the whole numbers in that range. Allow for the possibility that a user may start with a high number and end with a low number. Format the label so that commas are displayed and there are no decimal places showing. I need help with the codes.

    So far I'v come up with this

    For i = 1 to 1000

    Next

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: HELP with For Next...

    So, what's the problem. That looks OK to me.

    If you run into other problems, post them, and we'll help.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jan 2002
    Posts
    195

    Re: HELP with For Next...

    Public Function AddRange(ByVal LowNum As Long, ByVal HighNum As Long)as Long
    dim Result as Long
    Result = 0
    For i as integer = LowNum to HighNum
    Result += i
    Next i
    AddRange=Result
    End Function

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: HELP with For Next...

    Quote your post, and then MINE:

    Code:
    Public Function AddRange(ByVal LowNum As Long, ByVal HighNum As Long)as Long
      dim Result as Long
      Result = 0
      For i as integer = LowNum to HighNum
         Result += i
      Next i
      AddRange=Result
    End Function
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: HELP with For Next...

    Either you have to check BEFORE you call that sub, or FIRST THING inside of it.

    What if Low>High? Switch them...

    If you can't grasp these simple concepts, you should go back to the books
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: HELP with For Next...

    Right ... So far that is correct...

    Allow for the possibility that a user may start with a high number and end with a low number.
    Hint for this...

    Code:
    If LowNum > HighNum Then
    
    
    End If
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

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

Featured