Multiple Execution of Randomize statement

  • 14 years ago

    Dim monsternames As String() = {"", "Myr", "Omega", "Golem", "Pewter", "Hematite", "Leashling", "Silver Myr", "Copper Myr", "Phyrexian Colossus", "Cobalt", "Goblin", "Vorrac", "Titanium Golem", "Steel Wall", "Iron Myr"}

    Dim Randomizer As New Random

    Dim index As Integer = Randomizer.Next(0, monsternames.Length)

    Dim showmonster As String = monsternames(index)

    Label11.Text = showmonster


    This is my current code which randomly grabs a name from the list and displays it in a label. In this case Label 11.

    I want to repeat the randomization another 5 times and each time filling in the next label (the labels are from label11 to label16). I was wondering if I could put this in some sort of loop structure to fill in all the labels or do I have to write it out each time?

  • 14 years ago

    Sure, I would probably just create an array with your labels.  Then you could create a for loop and in that loop pick a random name and add it to the label using the values in the array.  An example would be clearer so here you go.  This example requires a form with 3 labels named label1, label2, label3 and a button.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myLabels As Label() = {Label1, Label2, Label3}

        For x As Integer = 0 To myLabels.Length - 1
            myLabels(x).Text = x.ToString
    End Sub</pre>

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.” - Dan Kaminsky