帮助随机字符串函数VB.NET?

时间:2011-07-31 20:17:19

标签: c# vb.net random

我在C#中创建了一个函数来创建一个随机字符串,但是我想将它转换为VB.NET,不幸的是我对Visual Basic的了解远远少于我对C#的了解。

这是我的VB.NET功能:

' 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()

正如您将看到的,它与我的C#版本没什么不同,只是因为VB可以使用可选参数,我允许用户选择要在字符串中使用的字符,或者只使用默认字符。

这是我的函数的C#版本:

    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, 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();

同样,没有太大的不同,但我真正努力的部分是在第12行VB代码和第13行C#代码之间的转换。

我真的不知道如何将其转换为VB.NET(正如我所说,我对它的了解非常有限),所以我使用了在线转换器。在线转换器的运行没有错误,但是当我尝试调用该函数时,不会出现任何字符串。

简而言之,这个C#代码工作正常: ReturnMe + = all [random.Next(0,all.Length)];

但是,这个VB.NET代码不起作用: ReturnMe + = Valid_Chars(随机。[Next](0,Valid_Chars.Length))

我如何修复VB.NET代码?

2 个答案:

答案 0 :(得分:3)

您的功能存在一些问题:

  • 你错过了while循环变量的增量(这样函数失败并出现OutOfMemoryException)
  • 您不应该连接字符串(即使在C#情况下)。请改用StringBuilder

此代码应该可以使用

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

答案 1 :(得分:1)

它与字符串索引操作没有任何关系,它是正确的。你只是忘了增加循环计数器。使用For关键字:

    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

如果Chars_In_String变得很大,那么StringBuilder会更明智。使用共享关键字,除非此代码位于模块内。