Results 1 to 5 of 5

Thread: [RESOLVED] loops for less times

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Resolved [RESOLVED] loops for less times

    Hey,

    The code below only loops for 3 times. I am assuming it should loop for six times right? Can someone tell me why please?

    Code:
     Private Sub RandomGenerator1()
            Dim AscChr As String, ChrAsc As String
            Dim counter As Integer = 0
            txtRandom1.Text = ""
            For counter = 0 To 5
    
                Randomize()
                AscChr = Abs(Int(64 - 122) * Rnd() - 64)
                ChrAsc = Chr(AscChr)
                txtRandom1.Text += String.Format(ChrAsc)
                counter += 1
            Next
        End Sub
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: loops for less times

    You dont need the line

    counter += 1

    as this is effectively incrementing the counter value twice

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: loops for less times

    heya,

    Got confused with the C# loop. Many thanks
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] loops for less times

    You're trying to create a random, 6-character string, correct?
    vb.net Code:
    1. Dim rng As New Random
    2. Dim min As Integer = Convert.ToInt32("A"c)
    3. Dim max As Integer = Convert.ToInt32("z"c) + 1
    4. Dim chars(5) As Char
    5.  
    6. For index As Integer = 0 To chars.GetUpperBound(0)
    7.     chars(index) = Convert.ToChar(rng.Next(min, max))
    8. Next
    9.  
    10. MessageBox.Show(New String(chars))
    Always use the Random class to generate random numbers, never Randomize() and Rnd().
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: [RESOLVED] loops for less times

    Ok ,

    Thanks for the advice. Can you pls tell me in why
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

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