Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello. I am trying to code a random password generator but I assume a problem.

Problem is:
VB
str2 = Conversions.ToString(random.Next(0, pool.Length))
            str = (str & Conversions.ToString(pool(Conversions.ToInteger(str2))))


What I have tried:

VB
Dim num As Integer = 0
        Me.TextBox1.Text = ""
        Dim str As String = ""
        Dim random As New Random
        Dim str2 As String = ""
        Do While (num < 3)
            str2 = Conversions.ToString(random.Next(0, pool.Length))
            str = (str & Conversions.ToString(pool(Conversions.ToInteger(str2))))
            num += 1
        Loop
        Me.TextBox1.Text = ("1618ML00" & str & "8")
Posted
Updated 24-Nov-18 13:59pm
Comments
MadMyche 24-Nov-18 21:04pm    
What is the problem? Is there an Exception being thrown?

1 solution

You might want to actually describe the problem. Just posting a line of code as the problem doesn't really say anything about what you think the problem is.

Also, that line of code itself IS a problem because it's so badly written, along with the entire method it's in.

It looks like you're trying to generate a random password using random indexes into an array of characters, correct?

A method should do exactly ONE thing. In this case, generate a string made up of random characters. It should NOT do anything with a textbox or preprend/append any characters other than the ones it's picking to the string.
'  Assume "pool" is an array of characters accessible by this method.
Public Function GeneratePasswordString(length As Integer) As String
    If length < 0 Then
        Throw New ArgumentException
    End If

    Dim RNG As New Random
    Dim buffer As New StringBuilder
    Dim index As Integer

    For count As Integer = 1 to length
        index = RNG.Next(0, pool.length)
        buffer.Append(pool(index))
    Next

    Return buffer.ToString
End Function
 
Share this answer
 
Comments
Member 14065947 25-Nov-18 1:45am    
What exactly I want is, click button1 to generate random numbers for example (123456xxxx) and only the (xxxx) will randomly generated and when it do it then show to textbox1.

Could you maybe help me out by writing the code?
Dave Kreskowiak 25-Nov-18 13:27pm    
I already did. It's up to you to do the very light work of putting a couple of strings together and putting that into the Text property of a textbox.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900