Are variable variables still not available on vbnet?

e.g

For i = 0 To 10
Me.Button[i].visible = false
next i

thanks

Recommended Answers

All 8 Replies

Those are called arrays. AFAIK yes, you can create arrays of buttons or other controls.

As far as your example is concerned the array would be redundant since you can use the Controls collection to do what you want:

For Each b As Button In Me.Controls.OfType(Of Button)()
    b.Visible = False
Next

If necessary you can pick which buttons to make invisible by using a conditional to check for a specicfic string in the name:

If b.Name.Contain("MyFilter") Then

If you must have a collection of just the buttons, I would suggest a List as that gives you more options. you can create one and initialize in the declaration statement:

Dim ButtonList As List(Of Button) = Me.Controls.OfType(Of Button).ToList

that example is different than what I want.

I have 4 labels named labeltroca1, labeltroca2, etc

I want the application to choose which to use based on a variable that is passed into a function:
(how can I do that without IF or CASE ?) [the function also returns true or false in case you find it strange it being boolean]

Function excepc(ByVal ppvp As Double, ByVal apvp As Double, ByVal pqty As Single, ByVal aqty As Single, **ByVal qualp As Single**) As Boolean
        On Error GoTo MOSTRARERRO
       ** Dim labeltroca(3) As String**
        Dim pun As Double
        Dim aun As Double
        If IsNumeric(pqty) Then
            pun = ppvp / pqty
        Else
            excepc = False
            labeltroca**(qualp)**.text =  "qty1?"
            labeltroca**(qualp)**.BackColor = Color.Yellow
        End If

I tried creating an array and adding (qualp) but it doesn't accept text as a member.
I learned Pascal 20 years ago and it was easy to have variable variables.

Thanks

should I make a class?
And declare the array of that class instead of string?

AS OBJECT!!!
found the right option
Thanks

No.
Had to do it like this to worrk:

Function excepc(ByVal ppvp As Double, ByVal apvp As Double, ByVal pqty As Single, ByVal aqty As Single, ByVal qualp As Single) As Boolean
1:      On Error GoTo MOSTRARERRO
2:      Dim labeltroca(3) As Label
        labeltroca(0) = labeltroca1
        labeltroca(1) = labeltroca2
        labeltroca(2) = labeltroca3
        labeltroca(3) = labeltroca4
3:      Dim pun As Double
4:      Dim aun As Double

5:      If IsNumeric(pqty) Then
6:          pun = ppvp / pqty
7:      Else
8:          excepc = False
9:          labeltroca(qualp - 1).Text = "qty1?"
10:         labeltroca(qualp - 1).BackColor = Color.Yellow
11:     End If

See why we always harp on people to show the code. Your solution would have arrived much faster.

While your solution will work for a few controls you can see how unmanageable it would be if you had a hundred labels to fill. A for each loop and a conditional will allow for managing a much larger number:

Dim labeltroca As New List(Of Label)
For Each l As Label In Me.Controls.OfType(Of Label)()
    If l.Name.Contains("labeltroca") Then
        labeltroca.Add(l)
    End If
Next

Lists are basically dynamic arrays, but with much more options for managing the collection. You would use it exactly the same

labeltroca(qualp - 1).Text = "qty1?"
labeltroca(qualp - 1).BackColor = Color.Yellow

Glad you found a solution though. Please remember to mark this solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.