When defining a non-computed property, you specify name, type and default. The default, if you leave it blank, just has the greyed out hint, "(none)".
I took this to mean that in the case of objects with value semantics, it's the default value, e.g., 0 for Integer, "" for string.
But what is it in the case of an Array(Of Integer)? Would it be an empty array? Or Nothing? I presumed, an empty array.
Here's why I'm asking. I have an Array(Of Integer) private property defined as described above. The class constructor's job is to fill this array based on the contents of a passed string.
The easy / happy path is that the property is assigned like this:
_codePoints = s.ToCodePoints()
Simple assignment.
But another use case for this class is to start out with an empty array and repeatedly append to it.
In this case, the caller might specify a capacity to size the array to in advance, because the caller knows about how big the Array needs to be. So the constructor ends up like this:
If capacity > s.Length And _codePoints.Capacity < capacity Then
_codePoints.Reserve(capacity)
_codePoints.AppendAll(s.ToCodePoints())
Else
_codePoints = s.ToCodePoints()
End If
The passed string is s and capacity is an optional integer that defaults to s.Length. When the caller asks for more capacity and _codePoints.Capacity is smaller, I reserve the capacity and just append the code points to that array. I assume I must be appending to an empty Array(Of Integer) or I would get a NothingReference Error.
BUT. And this is really, REALLY weird.
I have a loop that creates a new instance of this object at the top of the loop, iterates six times. And on the 4th iteration, the initial contents of _codePoints is the value that was there after the 0th iteration was complete. It leaked through somehow. 35 elements already present.
I'm reusing the same variable but I'm assigning a new instance to it each time. It works 4 times (iterations 0 through 3) and then bleeds through on iteration 4, and goes back to normal on iteration 5. The only arguably unusual thing is that I pass the empty string (s in the constructor code) when I New up the instance. I'm starting from empty, so s.ToCodePoints() will itself produce an empty Array(Of String) and AppendAll() zero elements. The job of the calling loop is to append several strings from there on subsequent method calls.
I "fixed" it by inserting _codePoints.RemoveAll() just before the AppendAll() call in the constructor. But this has to be some kind of bug in the VM, no? Or is it somehow a bad connection between the user and the keyboard?