Question Why is a variable declared for text not the same as the property for the text?

Thequazi

Member
Joined
Nov 18, 2011
Messages
7
Programming Experience
Beginner
Question in long form:
When I have this:
VB.NET:
Dim input As String = Textbox1.text
Dim output As String = Textbox2.text
If input = 10 then output = "X"

I get no errors but it also doesn't change the text in textboxt2 to X

However, when I have this:
VB.NET:
Dim input As String = Textbox1.text
If input = 10 then textbox2.text = "X"

It changes the text just fine.
 
Hi,

Dim output as string = Textbox2.text is not a pointer assignment, it is by Val(ue) not by Ref(erence), your later example sets the property directly.
 
Hi,

Dim output as string = Textbox2.text is not a pointer assignment, it is by Val(ue) not by Ref(erence), your later example sets the property directly.

That's not completely correct. String is a class, i.e. a reference type, so after this line:
Dim output As String = TextBox2.Text
'output' actually does refer to the same String object as TextBox2.Text. A reference is not a pointer though. This line:
If input = 10 then output = "X"
replaces the reference stored in the variable to a different reference, so 'output' now refers to a different String object.

String is a bit special as far as classes go anyway, in that instances are immutable, i.e. cannot be changed, so even if you wanted to modify the original object, you couldn't.
 
Back
Top