tags:

views:

34

answers:

3
Private Function Token() As String

        Dim Length As Byte
        Length = 10
        Dim Chars As Char() = New Char() {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c, _
       "g"c, "h"c, "i"c, "j"c, "k"c, "l"c, _
        "m"c, "n"c, "o"c, "p"c, "q"c, "r"c, _
       "s"c, "t"c, "u"c, "v"c, "w"c, "x"c, _
        "y"c, "z"c, "A"c, "B"c, "C"c, "D"c, _
       "E"c, "F"c, "G"c, "H"c, "I"c, "J"c, _
       "K"c, "L"c, "M"c, "N"c, "O"c, "P"c, _
        "Q"c, "R"c, "S"c, "T"c, "U"c, "V"c, _
       "W"c, "X"c, "Y"c, "Z"c, "0"c, "1"c, _
       "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, _
       "8"c, "9"c}

        Dim [Str] As String = String.Empty
        Dim Random As New Random()

        For a As Byte = 0 To Length - 1
            [Str] += Chars(Random.[Next](0, 61))
        Next
        Return ([Str])
    End Function

txtpassword.Text = Token()

is the above bolded line a correct .. it s not working too am just 13 years old plz help me and the above code is used to generate a random password for users which should be displayed on the text box ,if possible in label which is more advicable

+1  A: 

Use any other variable name than [String], it is reserved keyword and might create problems and then try.

Sarfraz
s sir now i have added
do you still get an error? if yes what is that?
Sarfraz
s sir it s not showin any error but the password values r not displayed in the textbox
make sure that your function really returns some respone, try to find what Str contains.
Sarfraz
+2  A: 

1) Write some tests to see whether your Token method works or not. If you're using Visual Studio, you can even test it in the IDE. Set a breakpoint on the return statement in your Token method and use the Watch window to see what value will be returned.

2) Your txtpassword.Text = Token() statement seems to be correct. This poses the question whether txtpassword is actually a control on the form you're looking at, and is also visible. Is it? (If you created txtpassword dynamically, did you do a myForm.Controls.Add(txtpassword)?

(There are some other issues in your code btw. -- e.g. make Length a constant, rename your variables so they don't bear the names of in-built types or keywords, possibly find a better way to initialise that char array, etc. -- but that'd be another question.)

stakx
thanks a lot sir
+1  A: 

I just tested your code, it's working fine without any problem, just I put a textbox and a button on a form and copy/paste your function in the form class and put

txtpassword.Text = Token()

on button click...

here is the complete code:

Public Class Form1

    Private Function Token() As String

        Dim Length As Byte
        Length = 10
        Dim Chars As Char() = New Char() {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c, _
       "g"c, "h"c, "i"c, "j"c, "k"c, "l"c, _
        "m"c, "n"c, "o"c, "p"c, "q"c, "r"c, _
       "s"c, "t"c, "u"c, "v"c, "w"c, "x"c, _
        "y"c, "z"c, "A"c, "B"c, "C"c, "D"c, _
       "E"c, "F"c, "G"c, "H"c, "I"c, "J"c, _
       "K"c, "L"c, "M"c, "N"c, "O"c, "P"c, _
        "Q"c, "R"c, "S"c, "T"c, "U"c, "V"c, _
       "W"c, "X"c, "Y"c, "Z"c, "0"c, "1"c, _
       "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, _
       "8"c, "9"c}

        Dim [Str] As String = String.Empty
        Dim Random As New Random()

        For a As Byte = 0 To Length - 1
            [Str] += Chars(Random.[Next](0, 61))
        Next
        Return ([Str])
    End Function


    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        TextEdit1.Text = Token()
    End Sub
End Class
Wael Dalloul