Are variable variables still not available on vbnet?


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


Edited by nosfa

3 Years
Discussion Span
Last Post by tinstaafl
Featured Replies

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

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

Edited by tinstaafl


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
            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.



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


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
    End If

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.

Edited by tinstaafl

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.