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

C# VB.NET随机字符串函数的帮助?,c#,vb.net,random,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 RandomStrin

我用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()
    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();
正如您将看到的,它与我的C版本没有太大的不同,只是因为VB可以接受一个可选参数,所以我允许用户选择要在字符串中使用的字符,或者只使用默认字符

下面是我函数的C版本:

' 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()
    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[random.Next0,all.Length]

但是,此VB.NET代码不起作用: ReturnMe+=有效字符随机。[下一步]0,有效字符长度


如何修复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()
    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();
您缺少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

您的职能中有几个问题:

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

如果字符串中的字符变得更大,那么字符串生成器将是明智的。除非此代码位于模块内,否则请使用Shared关键字。

它与字符串索引操作无关,它是正确的。您只是忘记了增加循环计数器。使用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

如果字符串中的字符变得更大,那么字符串生成器将是明智的。除非此代码位于模块内,否则请使用Shared关键字。

@HillBilly.Developer:只是猜测,但可能是因为1个GUID不保证是随机的,2个GUID是固定长度的。@HillBilly.Developer:只是猜测,但可能是因为1个GUID不保证是随机的,2个GUID是固定长度的。