How to draw a variable from others?

2

My intention is for the code to pass through 9 TextBox checking if they are filled with names or empty.

For each filled TextBox would be added 1 in a count 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 as the count 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 run a random number between 1 and the variable "cont" and randomly select 3 names registered in the variables P. That is, it would then be necessary to call the variables P by the number that was found by the random and register in some label, etc.

    
asked by anonymous 04.09.2017 / 16:48

2 answers

3

I think there are several options for doing what you ask, I would suggest using Array for this, 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 have helped!

    
04.09.2017 / 17:59
2

You can call the instance using a string:

Dim NameOfMyClass As String = "ConsoleApplication1.MyClassA"
Dim MyInstance As Object = Activator.CreateInstance(Type.GetType(NameOfMyClass)) 
    
04.09.2017 / 17:21