The String class in .NET is immutable. That means that once an instance of the String class has been created, its content cannot change. Look at the following code:

Dim vTheString As String = "Some random text"
vTheString = "Some other text"

What happens here is that a string instance is created with the value "Some random text", and it is referenced by the variable vTheString. Then a new string instance is created with the value "Some other text", and the variable vTheString will be altered to reference that new string instance instead. This is usually not an issue, but there are things to be aware of here. An example:

Module AppMain

    Private Class JustForTesting
        Public Value As String
    End Class

    Sub Main()

        Dim vTestObject As New JustForTesting

        vTestObject.Value = "This is my test."
        AlterTestObject(vTestObject)

        Console.Write(vTestObject.Value)

    End Sub

    Private Sub AlterTestObject(ByVal testObject As JustForTesting)

        With testObject.Value

            If .StartsWith("This") Then
                testObject.Value = "Which" & .Substring(4)
            End If

            If .EndsWith(".") Then
                testObject.Value = .Substring(0, .Length - 1) & "?"
            End If

        End With

    End Sub

End Module

This code will create a JustForTesting instance, assign the string "This is my test." to the Value field, call a method that apparently replaces the word "This" with the word "Which" and also replaces the dot in the end with a questionmark. It then prints the content of the Value field to the console output. And what it prints is:

This is my test?

Why is that? Where did the replacement of "This" to "Which" go? This is where immutable comes into the picture. The AlterTestObject method starts off by setting up a With-block which is often considered good practice when you are about to access an object several times, in order to improve performance a little bit. When the with block is set up, it will reference the string instance of the Value field of the JustForTesting object that is passed to the method. The code then checks if that string instance starts with "This". It does, so the code will assign a new string, starting with "Which", followed by the string after character 4 in the string instance referenced by the with block (resulting in "Which is my test.").

Since the content of a string instance cannot change, a new string instance will be created and the JustForTesting object's Value field will be changed to reference that new instance. This is where it goes "wrong". The Value field is now referencing a new string instance, so the with-block and the Value field are now pointing at two different strings.

Next the code will check if the string referenced by the with-block (which is no longer the same string instance as the Value field) ends with a dot. It does, so the code will assign a new string, starting with the string referenced by the with block up to the second last character, followed by a questionmark (resulting in "This is my test?").

Conclusion: do not set up with-blocks which reference immutable objects, if the code within the block might alter their content.

Bookmark and Share

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading