Accessing a variable from a string

37 views Asked by At

I have got 16 buttons and i am generating a random umber from 1 - 16 but then i don't know how to use that number that I've generated to access the variable. I tried "Button" + Order(Ord) but couldn't then access the properties of the variable.

Sub game()
    Order(Ord) = ((15 * Rnd()) + 1)
    Console.Text = Order(Ord)

    test = Order(Ord)

    Target = "Button" + test

    Ord = Ord + 1

End Sub
1

There are 1 answers

0
Andrew Morton On

If you have a list of the buttons then you can select a list entry at random. Always remember that the first entry in a list or an array has an index of 0.

If you put all the buttons in a Panel, it can be easier to keep them organised: for example, you can get VB.NET to select all the buttons within that panel. Then you can also get it to count them for you, so if you ever change how many buttons there are, you won't need to go through the code changing every applicable instance of 15.

For a random number generator, you'll be better off using the .NET Random class instead of the old VB Rnd() function. You only need one instance of it, so it can be created at the same time as the main form.

  • Note: the random-generator in the code in the question will produce floating-point numbers like 11.2342 - I doubt you have a button with that number, although there is no indication of what the Order method does with the value.

Please start a new VB.NET Windows Forms project, add a Panel named "TheButtonPanel" to the default form, and as many buttons as you like, just for testing, to the panel.

I have added a simple handler to each of the buttons, so you can see some of the possibilities of getting the code to do work instead of you.

Public Class Form1

    Public rand As New Random()
    Public theButtons As List(Of Button)

    Sub Game()
        Dim nButtons = theButtons.Count()
        Dim n = rand.Next(0, nButtons)
        Dim selectedButton = theButtons(n)
        selectedButton.PerformClick()

    End Sub

    Private Sub bnClick(sender As Object, e As EventArgs)
        Dim bn = DirectCast(sender, Button)
        MessageBox.Show(bn.Text)

    End Sub

    Private Sub Init()
        theButtons = TheButtonPanel.Controls.OfType(Of Button).ToList()

        ' An example of doing something with the buttons:
        For Each bn In theButtons
            AddHandler bn.Click, AddressOf bnClick
        Next

        ' Other initialisation code.

    End Sub

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        Game()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Init()

    End Sub

End Class

When you run it, it will show the form and automatically click a random button, and the click handler will show you the value of the button's .Text property (because I wanted to put something in the Game() method). You can still click the buttons to run the click handler.

Finally, try to give controls more descriptive names than things like "TheButtonPanel"—I have no idea what would be a good name in your program.