2011-07-31 49 views
0

我做在C#中的函數來創建一個隨機字符串,但我想將其轉換爲VB.NET,不幸的是我的Visual Basic的知識比我的C#的知識少得多。幫助隨機字符串函數VB.NET?

這裏是我的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(); 

再次,沒有太多的不同,但我真的很掙扎在部分之間是什麼VB代碼的第12行的轉換,以及第13行C#代碼。

我真的不知道如何將它轉換爲VB.NET(正如我所說的,我對它的瞭解是非常有限的),所以我用了一個在線轉換器。在線轉換器的結果運行時沒有錯誤,但是當我嘗試調用該函數時,沒有字符串出現。簡而言之,這個C#代碼可以正常工作: ReturnMe + = all [random.Next(0,all.Length)];

然而,這VB.NET代碼不起作用: ReturnMe + = Valid_Chars(隨機[下一頁](0,Valid_Chars.Length)。)

我怎麼能修復我的VB.NET代碼?

+0

爲什麼不創建一個GUID? –

+0

@ HillBilly.Developer:只是猜測,但也許是因爲*(1)*的GUID不能保證是隨機的,和*(2)*的GUID是固定長度。 – LukeH

回答

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

它沒有任何與字符串索引操作,它是正確的。你只是忘記增加循環計數器。使用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將是明智的。除非此代碼位於模塊內,否則請使用共享關鍵字。