populate a ListBox with random strings

R

ryguy7272

I am trying to understand some code that I found online. Basically, this will
populate a ListBox with random strings. Here is the code:

Protected Sub PopulateButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim wordLen As Integer
Dim NWords As Integer = 9999
Dim rnd As System.Random
rnd = New System.Random()
Dim rndChar As Char
Dim thisWord As String
Dim i, j As Integer
For i = 0 To NWords
wordLen = CInt(rnd.NextDouble * 20 + 1)
thisWord = “â€
For j = 0 To wordLen
rndchar = Chr(65 + CInt(rnd.Next, 25))
thisWord = thisWord & rndChar
Next
ListBox1.Items.Add(thisWord)
Next
End Sub

I can’t get this running and I can understand how to resolve the error.
Error mssg:
Argument ‘CharCode’ must be with the range of -32768 to 65535

Can someone please explain what is wrong?

Thanks!
Ryan--
 
A

Armin Zingler

ryguy7272 said:
I am trying to understand some code that I found online. Basically, this will
populate a ListBox with random strings. Here is the code:

Protected Sub PopulateButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim wordLen As Integer
Dim NWords As Integer = 9999
Dim rnd As System.Random
rnd = New System.Random()
Dim rndChar As Char
Dim thisWord As String
Dim i, j As Integer
For i = 0 To NWords
wordLen = CInt(rnd.NextDouble * 20 + 1)
thisWord = “â€
For j = 0 To wordLen
rndchar = Chr(65 + CInt(rnd.Next, 25))
thisWord = thisWord & rndChar
Next
ListBox1.Items.Add(thisWord)
Next
End Sub

I can’t get this running and I can understand how to resolve the error.
Error mssg:
Argument ‘CharCode’ must be with the range of -32768 to 65535

Can someone please explain what is wrong?


I can't compile this. There is an error in the line

rndchar = Chr(65 + CInt(rnd.Next, 25))

I guess it's meant to be

rndChar = Chr(65 + CInt(rnd.Next(25)))

Is that right? However, it doesn't produce the error you're getting, so you
should post the real code first.
 
S

Sergey Poberezovskiy

Ryan,

all you have to do is to correct the following line:
rndchar = Chr(65 + CInt(rnd.Next, 25))

to become:
rndChar = Chr(65 + CInt(rnd.Next(25)))

Obviously, the person that provided the code, was just trying to show the
concept (without the IDE handy to correct the syntax)

Please let me know if you need more information
 

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