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
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
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)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.