|
-
Mar 27th, 2009, 05:20 PM
#1
Thread Starter
Addicted Member
Randomly Display Strings
Basically what i want to do is when a command button is clicked and it has run the code at then end i want a label to display one string out of many at random. How would i declare the various strings and then get it to display one at radnom. Any help will be appreciated.
-
Mar 27th, 2009, 05:28 PM
#2
Re: Randomly Display Strings
You can do it like this, add as many words as you want.
Code:
Dim rnd As New Random
Dim MyWords As New List(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Label1.Text = MyWords(rnd.Next(0, MyWords.Count))
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
MyWords.Add("Hello")
MyWords.Add("Cat")
MyWords.Add("Boat")
MyWords.Add("VB")
MyWords.Add("Forum")
End Sub
-
Mar 27th, 2009, 05:29 PM
#3
Re: Randomly Display Strings
Use a List, then use Random to pull a random string from the list.
Code:
Dim RandomStrings as List(Of String)
Dim Rand as New Random
RandomStrings.Add("Text1")
RandomStrings.Add("Text2")
Label1.Text = RandomStrings(Rand.Next(0, RandomStrings.Count))
edit: Bulldog beat me to it =P
-
Mar 28th, 2009, 07:26 AM
#4
Thread Starter
Addicted Member
Re: Randomly Display Strings
When i used the above code i am getting the following error message:
Variable 'RandomStrings' is used before it has been assigned a value. A null reference exception could result as runtime.
Any help would be appreciated. If possible could you also explain to me what the following part of the code actually means:
Code:
RandomStrings(Rand.Next(0, RandomStrings.Count))
-
Mar 28th, 2009, 07:43 AM
#5
Re: Randomly Display Strings
It is a warning, try this
Dim RandomStrings as New List(Of String)
-
Mar 28th, 2009, 08:24 AM
#6
Thread Starter
Addicted Member
Re: Randomly Display Strings
Ok thanks problem solved. Could someone please explain the above code to me if possible. Thanks
-
Mar 28th, 2009, 09:56 AM
#7
Re: Randomly Display Strings
 Originally Posted by mouse88
Ok thanks problem solved. Could someone please explain the above code to me if possible. Thanks
Sure. The very first thing you do is declare the variable, which I'm sure you know about. But the variable type is different. List(Of T) is a special data type, like an ArrayList, but that lets you make a list of a specific variable type. A string list would look like this if you could see the whole thing.
{"Spot 0", "Spot1", "Spot2"}
Lists have easy commands such as .Add to add something onto the end or .RemoveAt to remove a certain slot.
Code:
StringList.Add("Spot X")
StringList.RemoveAt(0)
If you did that to the first List, you would get:
{"Spot1", "Spot2", "Spot X"}
Add simply adds another string onto the end of the list, and remove will remove a string spot from wherever the number specifies. Now, usually you'd think 1 would be the first spot, but it's actually 0. So to remove the second slot you would use 1, and so on and so forth.
As for Random, to declare you have to make a variable for it.
Code:
Dim randomGenerator as New Random
Now you can make random numbers. To make one you use .Next.
Code:
randomGenerator.Next(0,10)
That code would give you a rand number between 0 and9. Not 0 and 10. The second number always needs to be 1 higher than what you want.
So, we can make List(Of String) and then we can use a random number to access a random String Spot in that List. Now, we can't just use .Next(0, 10) because what if the list isn't 10 spots long? So what you do is use the length of the List.
Code:
randomGenerator.Next(0, StringList.Count)
And that will give you a random number that represents a spot in the string List. Now, the reason you don't add 1 onto the end number in this instance is because it's already added. Since the starting index (spot) for a string list is 0, it offsets them all by 0. Here's an example:
List(Of Integer)
{10, 7, 8}
The 10 in the above list has an index of 0, not 1. The 7 has 1, and the 8 has 2. The length however is 3. So if you did a random number from 0 to 3 (or 4 to make sure 3 is included), if you actually got 3, then it wouldn't work because the index only has a slot 2.
So back to this:
Code:
randomGenerator.Next(0, StringList.Count)
You don't need to + 1 on the end because the count is already 1 higher than the index value that represents the last spot.
Then, finally, to get a value from a list, you have to specify which index, or spot. You do this with paranthesis and the number representing the index.
This would return the string that is in spot 1 of a string list:
{0, 1, 2}
So if you used it on the above, you would actually get the #1 because that's what's in slot 1.
So that's basically the run-down on everything we gave you, hope it make sense. Ask more questions if you want to know more about something.
-
Mar 28th, 2009, 10:12 AM
#8
Re: Randomly Display Strings
I will just make one point. The point of a List is array-like behaviour with the ability to add and remove at will. In this case you presumably don't need that ability. If the list of words isn't going to change then why do you need to use a List? If your list is static then you should just use an array. Arrays are not bad. Lists are better IF you need to be able to add and remove items dynamically. If you don't need to do that then the List, which is actually a wrapper for an array anyway, is pointless. The array is more efficient so you should use it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|