The manual states that Static variables only have scope within the method in which they are declared - but this doesn’t seem to be correct if the methods are overloaded.
Consider this call to these 2 methods:
Sub Pressed() Handles Pressed
Dim r1 As String = m1.getItem("foo")
System.DebugLog( r1)
Dim r2 As String = m1.getItem(1)
System.DebugLog( r2)
End Sub
Protected Function getItem(s as string) As String
Static di As Dictionary
If di = Nil Then
di = New dictionary
di.value("foo") = "fish"
End If
return di.Lookup(s, "nada")
End Function
Protected Function getItem(i as Integer) As string
Static di As Dictionary
If di = Nil Then
di = New dictionary
di.value(1) = "chips"
End If
Return di.Lookup(i, "nada")
End Function
I would expect the result to be “fish” and “chips”. But no, the result is “fish” and “nada”. Examining the content it seems the static di is shared between the two methods.
Yes, it would seem to be and it took a little while to discover what the problem was.
Noted, though this was just an example. In my case I have overloaded the same method over 20 times (using enumerations), each with about 10-20 dictionary entries - so now I have a big job to create a new name for each static - especially as find-change won’t find the right parts to change.
I can reproduce this with a Dictionary (and presumably any class), but not an integer, e.g., static di as integer = 1.
I think I’ve encountered this before without getting to the root cause, so this may have been the case for quite some time. If so, changing that behavior now could lead to breaking code, although I’d argue that it would actually fix already-broken code.
At the very least, this behavior should be documented. Have you filed Feedback?
Haven’t tested, but the purpose of the previous structure was to create the dictionary only once on first use (lazy) rather than creating it every time the function is called. The proposed structure would create a new dictionary every time and defeat the point of using a Static.
Protected Function getItem(i As Integer) As String
Static di As New Dictionary
If di.KeyCount = 0 Then
di.value(1) = "chips"
End If
Return di.Lookup(i, "nada")
End Function
Protected Function getItem(s As String) As String
Static di As New Dictionary
If di.KeyCount = 0 Then
di.value("foo") = "fish"
End If
Return di.Lookup(s, "nada")
End Function