Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# vs VB.NET Urgent

Status
Not open for further replies.

cyberdeminout

Programmer
Jul 19, 2005
37
US
I have converted following C# script( to vb.net. Can anyone let me know vb.net script is same as c#? Why am asking is am not getting fixed length Unique ID.
Any help greatly appreicated.

C#:
private string GetUniqueKey()
{
int maxSize = 8 ;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize ;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data) ;
size = maxSize ;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size) ;
foreach(byte b in data )
{ result.Append(chars[b % (chars.Length - 1)]); }
return result.ToString();
}


VB.NET

Function GetUniqueKey()
Dim MaxSize As Integer = 8
Dim chars(62) As Char
Dim a As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
chars = a.ToCharArray()
Dim size As Integer = MaxSize
Dim data(size) As Byte
Dim crypt As New RNGCryptoServiceProvider
crypt.GetNonZeroBytes(data)
Dim result As New StringBuilder(MaxSize)
Dim b As Byte
For Each b In data
result.Append(chars(b Mod (chars.Length - 1)))
Next
Console.WriteLine(result.ToString)
Return result.ToString()
End Function
 
It seems that when you declare an array in VB.NET it uses a 0 index array but with max index as the dimmension value.
So you should use
Code:
Dim MaxSize As Integer = 7 '(0-7) = 8 elements
'instead of
Dim MaxSize As Integer = 8 '(0-8) = 9 elements

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top