String array manipulation

QADUDE

Member
Joined
Dec 30, 2009
Messages
12
Programming Experience
3-5
I am developing a random password generator. Producing numbers by a random search of an Integer array is no problem, however when I try to use strings i.e. letters of the alphabet I hit problems. The number of characters needed for a password is input through selection from a list box. The code below shows the lines which give problems. A Random search of the string array only returns the index and not the element content. Any would most appreciate any help.
    Private Sub ButNumandAlpha_Click(sender As Object, e As EventArgs) Handles ButNumandAlpha.Click
        Dim r As Integer
        Dim a As New ArrayList
        Dim num As Integer
        Dim alpha As Integer
        

        a.Add("A")
        a.Add("B")
        a.Add("C")
        a.Add("D")
        a.Add("E")
        a.Add("F")
        a.Add("G")
        a.Add("H")
        a.Add("I")
        a.Add("J")
        a.Add("K")
        a.Add("L")
        a.Add("M")
        a.Add("N")
        a.Add("O")
        a.Add("P")
        a.Add("Q")
        a.Add("R")
        a.Add("S")
        a.Add("T")
        a.Add("U")
        a.Add("V")
        a.Add("W")
        a.Add("X")
        a.Add("Y")
        a.Add("Z")
        a.Add("a")
        a.Add("b")
        a.Add("c")
        a.Add("d")
        a.Add("e")
        a.Add("f")
        a.Add("g")
        a.Add("h")
        a.Add("i")
        a.Add("j")
        a.Add("k")
        a.Add("l")
        a.Add("m")
        a.Add("n")
        a.Add("o")
        a.Add("p")
        a.Add("q")
        a.Add("r")
        a.Add("s")
        a.Add("t")
        a.Add("u")
        a.Add("V")
        a.Add("w")
        a.Add("x")
        a.Add("y")
        a.Add("z")

        count = ListBox2.SelectedIndex

        r = (count Mod 2)

        If r = 0 Then
            num = count / 2
            For x = 0 To num - 1
                Dim value As Integer = CInt(Int((9 * Rnd())))
                p(x) = value
            Next
            alpha = count / 2



            For x = num + 1 To count + 1
                Dim value As String = CInt(Int((51 * Rnd())))
                a(x) = value
            Next
        End If

        password = (p(0) & p(1) & p(2) & p(3) & p(4) & p(5) & p(6) & p(7) & p(8) & p(9) & p(10) & p(11) & p(12) & p(13) & p(14) & p(15) & p(16) & p(17) & p(18) & p(19) & p(20))

        RichTextBox1.Text = password
    End Sub
 
Last edited by a moderator:
Hi,

There seem to be quite a few issues with the code that you have posted, not least of which is that you obviously have Option Explicit turned off, unless you have declared some variables elsewhere, and Option Strict turned off. You need to turn these both on now to help you write your code better and identify potential type conversion errors before they actually occur.

As to your issue, you are using old VB6 technology in addition to the fact that you are using declared variables of various types incorrectly? Your project should be going nuts at about this point?? Another point is that you do not even attempt to get the element from the specified index in the character array that you have created and finally your description of variables and what they are supposed to represent is terrible.

So, to give an example of how I would go about this.

1) First of all use the Random class in .NET to do any randomisation that you need. Have a look here:-

Random.Next Method (Int32) (System)

You could then declare a variable at the Class Level of your project as:-

Dim myRandomGenerator As New Random


2) The next thing to do is to create a collection of characters to choose from to create the password. Using a For Loop to create a list of characters you could say:-

Dim charList As New List(Of Char)
 
'Add Uppercase Letters
For charValue As Integer = 65 To 90
  charList.Add(ChrW(charValue))
Next
 
'Add Lowercase Letters
For charValue As Integer = 97 To 122
    charList.Add(ChrW(charValue))
Next


Using LINQ to Objects you could also say:-

Dim charList As List(Of Char) = Enumerable.Range(65, 26).Select(Function(x) ChrW(x)).Concat(Enumerable.Range(97, 26).Select(Function(x) ChrW(x))).ToList


Notice that we use the ChrW function to return the actual ASCII Character from its related ASCII integer value.

3) The next thing to do is to get the number of characters for the length of the password from your ListBox. This seems fine since you are using the SelectedIndex property but for this example I will just use a value of 10. i.e:-

Dim someListBoxNumber As Integer = 10


4) The final thing is to create your Random Password from the list of characters that you want to use. Using a standard For Loop we could say:-

Dim somePassword As String = Nothing
 
For allLettersRequired As Integer = 1 To someListBoxNumber
  somePassword &= charList(myRandomGenerator.Next(charList.Count))
Next


Notice how we get a Random letter from the list of characters by passing a Random Index to that list of characters and then we append that specific character to the Password string.

We can also do another small Cheat here with LINQ to Objects and use the Repeat Method of the IEnumarable interface to create a sequence and say:-

Dim somePassword As String = Enumerable.Repeat(0, someListBoxNumber).Select(Function(x) charList(myRandomGenerator.Next(charList.Count))).ToArray


I hope that this is detailed enough to give you something to think about but not too detailed to confuse you.

Cheers,

Ian
 
Random Password using combination of upper & lowercase letters and numbers

Here is a way to incorporate all the upper and lowercase letters and digits in a password. The code is written in a Console app but you can translate it into a Windows Forms app. All you would need is a single button to generate the code, a numeric up/down control for user to enter the length of the password, and a label or textbox to output the result. No array is needed, and no listbox. It uses the ASCII value of the random number with the ChrW() method going from 0 to 61. (62 is needed for the 2nd Next argument.) Note how the code goes directly to the digits and uppercase and lowercase letters, and skips over the ASCII values for various symbols in between.

VB.NET:
Module Module1

    Sub Main()
        Dim randnum As New Random
        Dim newrand, ascnum, nums As Integer
        Dim mypass As String = ""
        Console.Write("Enter number of characters in password:  ")
        Integer.TryParse(Console.ReadLine, nums)
        For x As Integer = 1 To nums
            newrand = randnum.Next(0, 62)
            If newrand < 10 Then
                ascnum = newrand + 48       'digits from 0 to 9
            ElseIf newrand > 35 Then
                ascnum = newrand + 61       'lowercase letters
            ElseIf newrand > 9 Then
                ascnum = newrand + 55       'uppercase letters
            End If
            mypass &= ChrW(ascnum)
        Next x
        Console.WriteLine(mypass)
        Console.ReadLine()
   
        'If you want more digits included in the password, use the following code instead:

        mypass = ""
        For x As Integer = 1 To nums
            newrand = randnum.Next(0, 72)
            If newrand < 10 Then
                ascnum = newrand + 48       'digits from 0 to 9
            ElseIf newrand < 20 Then
                ascnum = newrand + 38       'more of the same digits
            ElseIf newrand > 45 Then
                ascnum = newrand + 51       'lowercase letters
            ElseIf newrand > 19 Then
                ascnum = newrand + 45       'uppercase letters
            End If
            mypass &= ChrW(ascnum)
        Next x
        Console.WriteLine(mypass)
        Console.ReadLine()
    End Sub

End Module
 
Last edited:
Answered
Many thanks Ian and solitaire. I will need some time to understand what you have given me and more importantly why.
PS I am using VB2012 both Option explicit and option Strict were off but are now on.
 

Latest posts

Back
Top