dcsimg
Results 1 to 3 of 3

Thread: VS 2013 - Generate Random String

  1. #1

    Thread Starter
    Member
    Join Date
    May 2015
    Posts
    58

    VS 2013 - Generate Random String

    I wrote this code pretty quickly. I never had any formal coding training so please forgive the less than perfect coding style. It is fully function and allows the paramters of Length, and using both upper/lower case.

    Code:
    Private Function GenerateString(ByVal CharCount As Long, ByVal UseLCase As Boolean, ByVal CharSetToUse as Integer)
            Dim rnd, rnd2 As New Random
            Dim AZ As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            Dim Nums As String = "1234567890"
            Dim Special As String = "~!@#$%^&*()_+{}[]"
            Dim CharsToUse As New StringBuilder
            Dim NewString As New StringBuilder
            Select Case CharSetToUse
                Case 0
                    CharsToUse.Append(AZ)
                Case 1
                    CharsToUse.Append(AZ)
                    CharsToUse.Append(Nums)
                Case 2
                    CharsToUse.Append(AZ)
                    CharsToUse.Append(Nums)
                    CharsToUse.Append(Special)
            End Select
            For i = 0 To CharCount - 1
                Dim chars As String = rnd.Next(0, CharsToUse.Length)
                Dim ULCase As Integer = rnd2.Next(1, 3)
                Console.WriteLine(ULCase)
                If UseLCase = True Then
                    Select Case ULCase
                        Case 1
                            NewString.Append(LCase(CharsToUse.Chars(chars)))
                        Case 2
                            NewString.Append(UCase(CharsToUse.Chars(chars)))
                    End Select
                Else
                    NewString.Append(CharsToUse.Chars(chars))
                End If
    
    
            Next
            Return NewString.ToString
    Last edited by ByteKnight; Jun 1st, 2015 at 01:52 PM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,920

    Re: VS 2013 - Generate Random String

    The code looks to be tied to (i.e. requires) a combobox named cmbCharsToUse.
    That enumeration should probably be passed as a parameter to the Function, rather than hardcoded to a control outside the function.
    Also, there is no need to have two Random objects.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2015
    Posts
    58

    Re: VS 2013 - Generate Random String

    Thank you for pointing this out. I have editted the code. I added another parameter than can be specificed as 0, 1, or 2.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width