私はランダムな文字列を作成するために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()
VBは、オプションのパラメータを取ることができますので、あなたはそれを除いて、私のC#バージョンからの大差ない、見るように、私は、ユーザーが何を選択することができます文字列内で使用する文字を使用するか、デフォルトの文字を使用します。ここで
は私の関数の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 [random.Next(0、all.Length)];
しかし、このVB.NETのコードは動作しません。 ReturnMe + = Valid_Chars(ランダム[次へ](0、Valid_Chars.Length))
私はVB.NETのコードを修正することができますか?
なぜGUIDを作成しないのですか? –
@ HillBilly.Developer:ちょうど推測ですが、*(1)* GUIDがランダムであることが保証されていない可能性があります。*(2)* GUIDは固定長です。 – LukeH