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
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.
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
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
Lists are basically dynamic arrays, but with much more options for managing the collection. You would use it exactly the same
For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
For Each subCtrl As Control In ctrl.Controls
If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
If subCtrl.GetType Is ...