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

本文关键字:VB NET 函数 字符串 随机 帮助 | 更新日期: 2023-09-27 18:04:00

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

这是我的VB。网络功能:

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

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

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

简而言之,这段c#代码工作得很好:ReturnMe += all[随机]。下一个(0,all.Length)];

然而,这个VB。. NET代码不工作:ReturnMe += Valid_Chars(随机)(下)(0,Valid_Chars.Length)

如何修复我的VB。NET代码?

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

你的函数有几个问题:

  • 你错过了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

它与字符串索引操作没有任何关系,它是正确的。您只是忘记了增加循环计数器。使用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将是明智的。使用Shared关键字,除非该代码位于模块内。