Dim txtLinqSum1Ad, txtLinqSum2Ad, txtLinqSum3Ad, txtLinqSum4Ad as String
instead of writing this thing
TextBox1.AppendText(txtLinqSum1Ad & vbNewLine)
TextBox1.AppendText(txtLinqSum2Ad & vbNewLine)
TextBox1.AppendText(txtLinqSum3Ad & vbNewLine)
TextBox1.AppendText(txtLinqSum4Ad & vbNewLine)
I would like to do something like that
For i as integer = 0 to 3
TextBox1.AppendText(txtLinqSum & i & Ad & vbNewLine)
Next
Do you think that could be possible? it would be useful when I have a lot of strings?
CodePudding user response:
Yes, it's possible with CallByName()
, or via Reflection (much longer syntax).
For CallByName()
to work, though, your variables have to be PUBLIC at Form/Class level:
Public Class Form1
Public txtLinqSum1Ad, txtLinqSum2Ad, txtLinqSum3Ad, txtLinqSum4Ad As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtLinqSum1Ad = "A"
txtLinqSum2Ad = "B"
txtLinqSum3Ad = "C"
txtLinqSum4Ad = "D"
End Sub
Private Sub butBereken_Click(sender As Object, e As EventArgs) Handles butBereken.Click
For i As Integer = 1 To 4
Dim s As String = CallByName(Me, "txtLinqSum" & i & "Ad", CallType.Get)
TextBox1.AppendText(s & vbNewLine)
Next
End Sub
End Class
But just because you can, doesn't mean you should. An array or other type of collection (List/Dictionary) would probably be a better choice.