HELP a newbie : help with rnd function

R

Robert

Hi

I am pretty new to prgramming and i'm having a spot of trouble with one
of my programs, i would like to, when a condition occurs chnge the property
of label1.text to a random letter of the alphabet

i have made an array called alphabet(25) for all letters.
here is what i have so far

dim alphabet(25) as string < this is declared outside a private sub, the
rest is inside one
alphabet(0) = ("a")

alphabet(1) = ("b")

alphabet(2) = ("C")

alphabet(3) = ("d")

alphabet(4) = ("e")

alphabet(5) = ("f")

alphabet(6) = ("g")

alphabet(7) = ("h")

alphabet(8) = ("i")

alphabet(9) = ("j")

alphabet(10) = ("k")

alphabet(11) = ("l")

alphabet(12) = ("m")

alphabet(13) = ("n")

alphabet(14) = ("o")

alphabet(15) = ("p")

alphabet(16) = ("q")

alphabet(17) = ("r")

alphabet(18) = ("s")

alphabet(19) = ("t")

alphabet(20) = ("u")

alphabet(21) = ("v")

alphabet(22) = ("w")

alphabet(23) = ("x")

alphabet(24) = ("y")

alphabet(25) = ("z")

IF <condition> then
Label2.Text = ("GO!")

label1.text = (Rnd(alphabet(25))) <<<<<< this is the line i am having
trouble with

TextBox1.Text = ("")

end if

i get an error on debug saying

'cast from string "a" to type 'single' is not valid

help would be very much appreciated

thanks Robert
 
M

Morten Wennevik

Hi Robert,

Rnd takes a single (number) as parameter, and does not understand alphabet(25) which is actually the string value "a"

Dim rand As New Random()
Label1.Text = alphabet(rand.Next(alphabet.Length)) '<<<<<< this isthe line i am having

This generates a random number in the range of your string < rand.Next(alphabet.Length) > and takes the letter in the position of the random number and puts it in Label1. I use the Random class as it is part of .Net, and Rnd is only part of VB.Net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top