D Dimitris999 New Member Feb 21, 2013 #1 In BeginnersGuide, on page 161 says: 'All other types including arrays of primitives types and strings are categorized as non-primitive types. When you pass a non-primitive to a sub or when you assign it to a different variable, a copy of the reference is passed'. I try to see what happen to strings with the following code: Code: Copy Dim Stra As String Dim Strb As String Stra = "a" Strb = Stra Strb = "b" Log (Stra) Log (Strb) Stra prints "a" and Strb prints "b". The value of Stra not changed after changing Strb. They Remain two separate variables. I try the above code using Stringbuilder instead of String Code: Copy Dim Stra As StringBuilder Dim Strb As StringBuilder Stra.Initialize Strb.Initialize Stra.Append("a") Strb = Stra Strb.Append("b") Log (Stra) Log (Strb) I see that both two variables are print "ab". So, the String variable types are primitive and when assign to a different variable or a sub copy their contents (pass by value) while Stringbuilder is non-primitive and copy the reference and not the content (pass by reference) ? Is this true?
In BeginnersGuide, on page 161 says: 'All other types including arrays of primitives types and strings are categorized as non-primitive types. When you pass a non-primitive to a sub or when you assign it to a different variable, a copy of the reference is passed'. I try to see what happen to strings with the following code: Code: Copy Dim Stra As String Dim Strb As String Stra = "a" Strb = Stra Strb = "b" Log (Stra) Log (Strb) Stra prints "a" and Strb prints "b". The value of Stra not changed after changing Strb. They Remain two separate variables. I try the above code using Stringbuilder instead of String Code: Copy Dim Stra As StringBuilder Dim Strb As StringBuilder Stra.Initialize Strb.Initialize Stra.Append("a") Strb = Stra Strb.Append("b") Log (Stra) Log (Strb) I see that both two variables are print "ab". So, the String variable types are primitive and when assign to a different variable or a sub copy their contents (pass by value) while Stringbuilder is non-primitive and copy the reference and not the content (pass by reference) ? Is this true?
Erel B4X founder Staff member Licensed User Longtime User Feb 22, 2013 #2 It is more correct to say that Strings are immutables and StringBuilders are mutables. Upvote 0
D Dimitris999 New Member Feb 22, 2013 #3 I think I understand now. If I do understand correctly, in my first example code: Code: Copy Strb = Stra 'After this line both Stra and Strb references to the same String Object that contains “a” Code: Copy Strb = "b" 'Because the strings are immutable and so it is not possible the modifying of the object that contains "a", an new object is created that contains “b” and Strb references to this new object. The Stra still references to the String Object that containing “a” Is this correct? Thank you in advance. Upvote 0
I think I understand now. If I do understand correctly, in my first example code: Code: Copy Strb = Stra 'After this line both Stra and Strb references to the same String Object that contains “a” Code: Copy Strb = "b" 'Because the strings are immutable and so it is not possible the modifying of the object that contains "a", an new object is created that contains “b” and Strb references to this new object. The Stra still references to the String Object that containing “a” Is this correct? Thank you in advance.