开发者

Help with random string function VB.NET?

开发者 https://www.devze.com 2023-03-24 19:10 出处:网络
I\'ve made a function in C# to create a random string, but I wanted to convert it to VB.NET, unfortunately my knowledge of Visual Basic is much less than my knowledge of C#.

I've made a function in C# to create a random string, but I wanted to convert it to VB.NET, unfortunately my knowledge of Visual Basic is much less than my knowledge of C#.

Here is my VB.NET function:

' Function will take in the number of characters in the string, as well as the optional parameter of chars to use in the random string
    Private Function RandomString(ByVal Chars_In_String As Integer, Optional ByVal Valid_Chars As String = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")
        ' Create a string to hold the resulting random string
        Dim ReturnMe As String = ""

        ' Loop variable
        Dim i As Integer = 0

        ' Run while loop while i is less than the desired number of Chars_In_String
        While i < Chars_In_String
            ' Each time through, add to ReturnMe (selecting a random character out of the string of all valid characters)
            ReturnMe += Valid_Chars(random.[Next](0, Valid_Chars.Length))
        End While

        ' Return the value of ReturnMe
        Return ReturnMe
    End Function
    ' Create a new instance of the Random class, using a time-dependant default seed value
    Dim random As New Random()

As you will see, its not much different from my C# version, except that since VB can take an optional parameter, I allow the user to select what characters to use in the string, or just use the default ones.

Here's the C# version of my function:

    private static string RandomString(int Chars_In_String)
    {
        // Create a string to contain all valid characters (in this case, just letters)
        string all = "qwertyuiopasdfghjklzxcvbnmQWERTYIOPASDFGHJKLZXCVBNM";

        // Create a variable that will be returned, it will hold the random string
        string ReturnMe = "";

        // Run for loop until we have reached the desired number of Chars_In_String
        for (int i = 0; i < Chars_In_String; i++)
        {
            // Each time through开发者_C百科, add to ReturnMe (selecting a random character out of the string of all valid characters)
            ReturnMe += all[random.Next(0, all.Length)];
        } 

        // Return the value of ReturnMe
        return ReturnMe;
    }
    // Create a new instance of the Random class, using a time-dependant default seed value
    static Random random = new Random();

Again, there's not much different, but the part I'm really struggling on is the conversion between what is the 12th line of VB code, and the 13th line of C# code.

I didn't really know how to convert it to VB.NET (as I said, my knowledge of it is very limited), so I used an online converter. The result of the online converter runs with no errors, however when I try to call the function, no string appears.

In short, this C# code works fine: ReturnMe += all[random.Next(0, all.Length)];

However, this VB.NET code doesn't work: ReturnMe += Valid_Chars(random.[Next](0, Valid_Chars.Length))

How could I fix my VB.NET code?


There are a few issues in your function:

  • You're missing the increment of the while loop variable (this way the function fails with an OutOfMemoryException)
  • You shouldn't concatenate strings (even in the C# case). Use a StringBuilder instead.

This code should work

Private Shared Function RandomString(ByVal Chars_In_String As Integer, Optional ByVal Valid_Chars As String = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM") As String
    Dim sb As StringBuilder = new StringBuilder()

    Dim i As Integer = 0
    Dim random As New Random()

    While i < Chars_In_String
        sb.Append(Valid_Chars(random.[Next](0, Valid_Chars.Length)))
        i = i + 1
    End While

    Return sb.ToString()
End Function


It doesn't have anything to do with the string indexing operation, it is correct. You are simply forgetting to increment the loop counter. Use the For keyword:

    Dim ReturnMe As String = ""
    For i As Integer = 1 To Chars_In_String
        ReturnMe += Valid_Chars(random.Next(0, Valid_Chars.Length))
    Next
    Return ReturnMe

A StringBuilder would be wise if Chars_In_String ever gets largish. Use the Shared keyword unless this code lives inside a Module.

0

精彩评论

暂无评论...
验证码 换一张
取 消