Jump to content
Tuts 4 You

Error generating a random letter in VB.NET


grizzmo

Recommended Posts

Hope someone can give me a hint, because I have the following code copied from reflector(VB.NET) into vb express edition 2010, but I have a lot of errors, so I started fixing the code, but i ended with an error, which it seems I , not yet, can't resolve myself. Here she comes:


Private Function genRandomLetter(ByVal rand As Random) As CharReturn IIf((rand.Next(1, 3) = 1), DirectCast(rand.Next(&H41, 90), Char), DirectCast(rand.Next(&H61, &H7A), Char)))End Function

And here is the error:


Error 2 'Integer' values cannot be converted to 'Char'. Use 'Microsoft.VisualBasic.ChrW' to interpret a numeric value as a Unicode character or first convert it to 'String' to produce a digit.

This solution offered seems to be impossible, only error after error.

So, I tried to declare "Char" as string, as Integer and as Object, but unfortunately, "Char" can't be used.

So I used "x" or whatever,instead of "Char" as char/string/integer, no good again.

This solution doesn't work for me at all!

Here are the Imports I included:


Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Windows.Forms
Imports System.Security.Cryptography
Imports System.Text
Imports System.ComponentModel
Imports System.Globalization
Imports System.Threading
Imports System.Windows
Imports Microsoft.VisualBasic.CompilerServices

That's about it, any tips are welcome.

Thnx

grizzmo

Link to comment
    Public Function GenerateRS(ByVal Length As Int32) As String        Dim Base As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"        Dim Chars() As Char = Base.ToCharArray        Dim FinalString As New System.Text.StringBuilder(Length)        Dim RN As New Random(Now.Millisecond)        For i As Int32 = 1 To Length            FinalString.Append(Chars(RN.Next(0, Chars.Length - 1)))        Next        Return FinalString.ToString    End Function
Link to comment

Thanks for the code Kurapica,

I made my own solution with someones else help, here she comes:


Private Function genRandomLetter(ByVal rand As Random) As Char Return IIf((rand.Next(1, 3) = 1), Convert.ToChar(rand.Next(&H41, 90)), Convert.ToChar(rand.Next(&H61, &H7A))) End Function

Also convert Integer to Char, whithout any probs.

Have a nice day and thanks for your time.

Edited by grizzmo
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...