Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1898642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:47:27+00:00 2026-05-17T06:47:27+00:00

learning about loops (still a beginner) in VB.net. I have got the below code

  • 0

learning about loops (still a beginner) in VB.net. I have got the below code and basically it is meant to stop the loop once it reaches a number above 20. Now, the problem is that it does stop after number 20 but the last number that is displayed in the list is always above 20…..any idea how I can stop it showing the last number as above 20?

If you guys know it can you also explain a bit about the answer – would like to learn as well as have the code..Thanks!

Private Sub btnDoWhileLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoWhileLoop.Click
    Dim objRandom As New Random
    Dim intRandomNumber As Integer = 0
    ClearList()

    Do While intRandomNumber < 20
        intRandomNumber = objRandom.Next(25)
        lstData.Items.Add(intRandomNumber.ToString)
    Loop
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:47:27+00:00Added an answer on May 17, 2026 at 6:47 am

    The problem is that you are adding the random item immediately after it was generated, without checking whether you should add it.

    You can check that the number should be added to the list before adding it:

    Private Sub btnDoWhileLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoWhileLoop.Click
        Dim objRandom As New Random
        Dim intRandomNumber As Integer = 0
        ClearList()
    
        Do While intRandomNumber < 20
            intRandomNumber = objRandom.Next(25)
            If intRandomNumber < 20 Then
                lstData.Items.Add(intRandomNumber.ToString)
            End If
        Loop
    End Sub
    

    Or, break out of the loop:

    Private Sub btnDoWhileLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoWhileLoop.Click
        Dim objRandom As New Random
        Dim intRandomNumber As Integer = 0
        ClearList()
    
        Do While intRandomNumber < 20
            intRandomNumber = objRandom.Next(25)
            If intRandomNumber >= 20 Then
                Exit While
            End If
    
            lstData.Items.Add(intRandomNumber.ToString)
        Loop
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just been learning about master pages in ASP.NET 2.0. They sound great, but
I have been learning about the basics of C# but haven't come across a
I am just learning about app.config in respect of creating custom sections. I have
I'm still learning about REST and, in my test, came up with this scenario
I'm still learning (baby steps). Messing about with a function and hoping to find
I'm learning about Win32 programming, and the WinMain prototype looks like: int WINAPI WinMain
I'm learning about table design in SQL and I'm wonder how to create a
I remember first learning about vectors in the STL and after some time, I
Where should I start learning about version control systems? I've used SVN, Team Foundation,
I'm interesting in learning about the different layers of abstraction available for making graphical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.