Returnvalue of uninitialized variables
Posted: Thu Dec 23, 2021 2:22 pm
We use a VO created function Array2String which returns an array as a delimiter separated string. In a specific situation, the array was empty which returns an empty string in VO, but apparently a nil value in X#.
I know that in .Net we are supposed to initialize all variables but in this case the compiler did not warn; by adding the 2 bold lines it works fine again. Should the compiler warn that I might return an uninitialized variable? The function does not return a proper empty string either.
Is this expected, or something we can prevent somehow? I can imagine that a lot of converted code with uninitialized variables would work in VO but not in X# as I found today.
Below the function
Dick
[hr]
Function Array2String( aArray As Array, cSep As String ) As String
Local cString As String
Local nMax, i As Dword
nMax := ALen( aArray )
For i := 1 Upto nMax
cString := cString + AsString( aArray[ i ] ) + cSep
Next i
If SLen( cString) > 0
cString := Left( cString, SLen( cString ) - 1 )
Else
cString:="" // 23-12-2021 Otherwise cString does not return the right value
Endif
Return cString
I know that in .Net we are supposed to initialize all variables but in this case the compiler did not warn; by adding the 2 bold lines it works fine again. Should the compiler warn that I might return an uninitialized variable? The function does not return a proper empty string either.
Is this expected, or something we can prevent somehow? I can imagine that a lot of converted code with uninitialized variables would work in VO but not in X# as I found today.
Below the function
Dick
[hr]
Function Array2String( aArray As Array, cSep As String ) As String
Local cString As String
Local nMax, i As Dword
nMax := ALen( aArray )
For i := 1 Upto nMax
cString := cString + AsString( aArray[ i ] ) + cSep
Next i
If SLen( cString) > 0
cString := Left( cString, SLen( cString ) - 1 )
Else
cString:="" // 23-12-2021 Otherwise cString does not return the right value
Endif
Return cString