How to call a variable from others?

Asked

Viewed 910 times

2

My intention is that the code goes through 9 Textbox checking if they are filled with names or empty.

For each Textbox filled would be added 1 in a counting variable and would be allocated the text of the Textbox in another variable (P1,P2,P3..... P9).

The problem is: how to allocate the text in the variables p, considering the final number the same number of the counting variable?

Ex.:

Dim Cont As Integer
Dim P1, P2, P3, P4, P5, P6, P7, P8, P9 As String

If TextBox1.Text <> "" Then
   Cont = Cont + 1
   ("P"&Cont) = TextBox1.Text <<< Essa parte que não sei escrever
End If

The intention at the end is to rotate a random number between 1 and the variable "cont" and randomly select 3 names registered in the variables P. That is, then it would be necessary to call the variables P by the number that was found by the random and register in some label, etc.

  • 2

    Why don’t you create a array with these names and then search the indexes randomly?

2 answers

3


I believe that there are several options to ask what question, I would suggest using a Array to that end, as follows:

Dim CAMPO As Control
Dim DADOS As Variant
Dim i As Integer

' Configura seu array com a quantidade de dados '9' no caso citado
ReDim DADOS(9, 2) As String

    i = 1

    For Each CAMPO In Me.Controls
        ' Verifica se é do tipo TextBox
        If TypeName(CAMPO) = "TextBox" Then

            ' Aqui pode-se verificar se 'CAMPO' está vazio e dar uma mensagem ao usuário, por exemplo
            ' if CAMPO = "" then
            '     MsgBox "ups"
            '     Exit Sub ' Ou Function...
            'End if

            DADOS(i, 1) = CAMPO.Name
            DADOS(i, 2) = CAMPO
        End If
     Next

I hope I’ve helped!

  • Thank you very much Evert, I still didn’t know how to use arrays, but the LINQ comment made me research on the topic and its response complemented with what I needed to solve the problem. In these queries, I also found that you can put all textboxes inside a frame and call them in the array structure as follows: Frame1.Controls(0) <<< would be zero to the number of elements within the frame. Thank you all, you helped me so much! Hugs

  • 1

    Thanks! Good luck there!

2

You can call the instance using a string:

Dim NameOfMyClass As String = "ConsoleApplication1.MyClassA"
Dim MyInstance As Object = Activator.CreateInstance(Type.GetType(NameOfMyClass)) 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.