Multiple links pointing to the same string?
Is the following simplification possible in VB.NET?
Example: a variable text
pointing to another line:
Class Form1 Sub New() Dim text As (what_type?) = AddressOf TextBox1.Text 'simplification If text = "foo" Then text = "bar" 'actually accessing TextBox1.Text End Sub End Class
I think this is not possible, but I could be wrong.
+3
source to share
2 answers
VB.NET has no pointers. You can use properties :
Public Property Text As String
Get
Return TextBox1.Text
End Get
Set(value As String)
TextBox1.Text = value
End Set
End Property
You can use properties as a layer so that you don't expose the control itself, just the relevant information:
If Text = "foo" Then Text = "bar"
This way you can even change the control (fe before Label
) without breaking your code.
Another approach uses a lambda expression :
Dim setText = Sub(str As String) TextBox1.Text = str
setText("test")
Dim getText = Function() TextBox1.Text
Dim text As String = getText()
+3
source to share
In the local area:
Using a locally declared function and procedure (any of them are optional):
Public Class Form1 Sub New() Dim text As Func(Of String) = Function() TextBox1.Text Dim setText As Action(Of String) = Sub(value) TextBox1.Text = value If text() = "foo" Then setText("bar") End Sub End Class
0
source to share