0

Can anyone help me to solve this problem? How to validate ?Thank you

Home identification code (4 alphanumeric characters) – required
Program should generate it using a random number generator

2
Contributors
1
Reply
16
Views
3 Years
Discussion Span
Last Post by Reverend Jim
0

Try

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.Text = GenerateKey(4, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    End Sub

    Private Function GenerateKey(nchars As Integer, fromStr As String) As String

        Dim rnd As New Random()
        Dim key As String = ""

        Do While key.Length < nchars
            key &= fromStr.Substring(rnd.Next(0, fromStr.Length), 1)
        Loop

        Return key

    End Function

End Class

The Function, GenerateKey will generate a random string of the requested length from the given string. The segment

rnd.Next(0, fromStr.Length)

generates an index for selecting a random character from the string. If you want a different source string you can use

Const FROMSTR = "ABCDEFGHIJKMLMOPQRSTUVWXYZ" &
                "abcdefghijklmnopqrstuvwxyz" &
                "0123456789"

Dim key As String = GenerateKey(4, FROMSTR)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.