This problem is not exclusively due to the declaration of a reference, but more generally due to the initializer of the declaration
'Dim Shared' which must be a constant (or expression which can be evaluated at compile time, or global variable which can be evaluated at compilation).
See the
SHARED documentation page:
- dim shared s as T
works, but:
dim shared s as T = *p
does not work (constant expected).
For an
'UDT' instance to be able to serve as an initializer for
'Dim Shared', it must be global (therefore static declaration by consequence) and it must can to be evaluated at compile time (therefore a constructor must be defined explicitly).
Example:
Code: Select all
type T
dValue as double
declare constructor ()
End Type
constructor T
end constructor
dim shared t1 as T
dim shared t2 as T = t1
dim shared byref t3 as T = t1
- Note:
- - 'dim shared byref t3 as T = t1' without constructor works because no new global instance is created, only a global reference to an existing global instance,
- declare an explicit constructor can be replaced with a member data initializer (as 'dValue as double = 0')
But in this case,
'New' usage is disallowed because such an initializer instance is dynamic (so not static).
One can never define a global reference to a dynamic variable, but only a local reference.