0

In Visual Basic, control elements such as textboxes seem to act like objects, and therefore if I assign a variable to such an element, it seems to be assigned by reference. For example:

TextBox1.Text = "old text"
Dim ctrlEl as Object = TextBox1
ctrlEl.Text = "new text"
Debug.print(TextBox1.Text)
'prints "new text"

However, if I assign any particular property of that control, it seems to be assigned by value. Like this:

TextBox1.Text = "old text"
Dim ctrlElText as Object = TextBox1.Text
ctrlElText = "new text"
Debug.print(TextBox1.Text)
'prints "old text"

But what I need is to assign ctrlElText to TextBox1.Text by reference, so that modifying ctrlElText would update the TextBox1.Text as well. How can I do that?

  • `TextBox1.Text` is not a variable. There is no such thing as a reference to it. (Technical explanation: `.Text` is a *property* of `TextBox1`. `TextBox1.Text` is a function call, it returns a value.) – AlexP Dec 29 '18 at 22:41
  • @AlexP, thank you for the explanation. In that case, how can I assign the ctrlElText to that function call, so that changing the value of ctrlElText would modify what text is displayed in that TextBox? – Justinas Rubinovas Dec 29 '18 at 22:57
  • You pass the *object* `TextBox1` to the function. – AlexP Dec 30 '18 at 00:35
  • Yes, see, in my case, I have a function that has receives a whole array of these objects - not just TextBoxes, but also CheckBoxes, ComboBoxes, etc. I want to specify which parameter of these objects the function has to modify - in case of TextBox it is .Text, in case of CheckBox - .Checked, etc. I want to specify that as I'm passing it to the function, rather than having the function itself figure it out. That is why I wanted to pass TextBox1.Text. How can I do that? – Justinas Rubinovas Dec 30 '18 at 10:45

0 Answers0