xsharp.eu • .net is a extremly dangerous thing ;-) - Page 2
Page 2 of 2

.net is a extremly dangerous thing ;-)

Posted: Wed Aug 29, 2018 5:59 pm
by Chris
Hi Karl-Heinz,
Karl-Heinz wrote: So this isn´t save enough ?

Code: Select all

aDouble [ 1 ] := (double) -1223.56  
aDouble [ 2 ] := (double) 7869
aDouble [ 3 ] := (double) aDouble [ 1 ] + (double) aDouble [ 2 ]
If aDouble is a an array of Double (and not array of Object), then it is absolutely safe, everything is known at compile time. But this wouldn't be safe, as the type of the oNumeric var is not known at compile time:

Code: Select all

LOCAL oNumeric AS OBJECT
o := 1234
aDouble [ 1 ] := (double) oNumeric
In this case, the compiler cannot make any conversion, as it does not know from what type to convert to a Double. So it assumes oNumeric really holds a double, which fails at runtime because it actually holds an INT.

Btw, a note to everybody, this is not a compatibility problem with VO at all, because in VO you could not put numeric values into an OBJECT var...

Chris

.net is a extremly dangerous thing ;-)

Posted: Wed Aug 29, 2018 8:55 pm
by Karl-Heinz
Hi Chris,

Code: Select all

LOCAL oNumeric AS OBJECT
o := 1234
aDouble [ 1 ] := (double) oNumeric
In this case, the compiler cannot make any conversion, as it does not know from what type to convert to a Double. So it assumes oNumeric really holds a double, which fails at runtime because it actually holds an INT.
ok, i got it. i enhanced my sample with a second array:

VAR aDouble2 := <OBJECT>{ -23 , 276.45}

the first Element holds an int, while the second element holds a float, Without to convert aDouble2[1] to a float the program crashes.

Code: Select all


FUNCTION DoConsole2() AS VOID
LOCAL fTotal AS double
VAR aDouble := <OBJECT>{0.00,276.45,276.45}
VAR aDouble2 := <OBJECT>{-23,276.45}


Console.WriteLine("{0,12:###,##0.00}" + "{1,12:###,##0.00}" + "{2,20:###,###,##0.00}" , aDouble) 
fTotal += (double) aDouble [ 3 ]

aDouble [ 1 ] := (double)  -1223.56  
aDouble [ 2 ] := (double) 122
aDouble [ 3 ] := (double) aDouble [ 1 ] + (double) aDouble [ 2 ]

Console.WriteLine("{0,12:###,##0.00}" + "{1,12:###,##0.00}" + "{2,20:###,###,##0.00}" , aDouble) 
fTotal += (double) aDouble [ 3 ] 

// aDouble [ 1 ] :=  (double) aDouble2 [ 1 ]  // this fails at runtime because aDouble2[1] holds an none float -> -23  
// aDouble [ 2 ] :=  (double) aDouble2 [ 2 ]  // this would work, because aDouble2[2] already holds a float -> 276.45

aDouble [ 1 ] := Convert.ToDouble  ( aDouble2 [ 1 ]  )  
aDouble [ 2 ] := Convert.ToDouble  ( aDouble2 [ 2 ]  )
aDouble [ 3 ] := (double) aDouble [ 1 ] + (double) aDouble [ 2 ]

Console.WriteLine("{0,12:###,##0.00}" + "{1,12:###,##0.00}" + "{2,20:###,###,##0.00}" , aDouble) 
fTotal += (double) aDouble [ 3 ] 

// 12 + 12 == 24
Console.WriteLine(space ( 24 ) + repl ( "-" , 20 ) )
// 12 + 12 + 20 == 44          

Console.WriteLine("{0,44:###,###,##0.00}" , fTotal )
                                                     

RETURN

regards
Karl-Heinz

.net is a extremly dangerous thing ;-)

Posted: Wed Aug 29, 2018 9:44 pm
by FFF
Karl-Heinz,
with this:
VAR aDouble2 := <OBJECT>{ -23 , 276.45}
i think, you FORCE the compiler to treat the numbers as OBJECT, i.e. you "forbid" it to be "clever" and deduct the type.

Karl

.net is a extremly dangerous thing ;-)

Posted: Thu Aug 30, 2018 4:49 am
by wriedmann
Hi Karl-Heinz,

IMHO the misunderstanding is here:
in VO we use

Code: Select all

float( _cast, uValue )
to cast, and

Code: Select all

float( uValue )
to convert.

In .NET,

Code: Select all

(double) oObject
is a cast, and not a conversion, and since we don't work on pointers anymore, the code crashes.

Wolfgang

.net is a extremly dangerous thing ;-)

Posted: Thu Aug 30, 2018 6:26 am
by Karl-Heinz
Hi Karl,
FFF wrote:Karl-Heinz,
with this:
VAR aDouble2 := <OBJECT>{ -23 , 276.45}
i think, you FORCE the compiler to treat the numbers as OBJECT, i.e. you "forbid" it to be "clever" and deduct the type.
Yes, that´s what i wanted ;-)

[...]

VAR aDouble2 := <OBJECT>{ -23 , 276.45}

the first Element holds an int, while the second element holds a float, Without to convert aDouble2[1] to a float the program crashes.

[...]

To round things off, i added another array, a <double> array,

Code: Select all


FUNCTION DoConsole2() AS VOID
LOCAL fTotal AS double
VAR aDouble := <OBJECT>{0.00,276.45,276.45}
VAR aDouble2 := <OBJECT>{-23,276.45}
VAR aDouble3 := <DOUBLE>{123 ,-1276.45}

Console.WriteLine("{0,12:###,##0.00}" + "{1,12:###,##0.00}" + "{2,20:###,###,##0.00}" , aDouble) 
fTotal += (double) aDouble [ 3 ]

aDouble [ 1 ] := (double)  -1223.56  
aDouble [ 2 ] := (double) 122
aDouble [ 3 ] := (double) aDouble [ 1 ] + (double) aDouble [ 2 ]

Console.WriteLine("{0,12:###,##0.00}" + "{1,12:###,##0.00}" + "{2,20:###,###,##0.00}" , aDouble) 
fTotal += (double) aDouble [ 3 ] 

// aDouble [ 1 ] :=  (double) aDouble2 [ 1 ]  // this fails because aDouble2[1] holds an none float -> -23  
// aDouble [ 2 ] :=  (double) aDouble2 [ 2 ]  // this would work, because aDouble2[1] holds already a float -> 276.45

aDouble [ 1 ] := Convert.ToDouble  ( aDouble2 [ 1 ]  )  
aDouble [ 2 ] := Convert.ToDouble  ( aDouble2 [ 2 ]  )
aDouble [ 3 ] := (double) aDouble [ 1 ] + (double) aDouble [ 2 ]

Console.WriteLine("{0,12:###,##0.00}" + "{1,12:###,##0.00}" + "{2,20:###,###,##0.00}" , aDouble) 
fTotal += (double) aDouble [ 3 ]

aDouble [ 1 ] := aDouble3 [ 1 ]  // <DOUBLE> array - no cast or convert necessary  > 123
aDouble [ 2 ] := aDouble3 [ 2 ]  // <DOUBLE> array - no cast or convert necessary  > -1276.45
aDouble [ 3 ] := (double) aDouble [ 1 ] + (double) aDouble [ 2 ]

Console.WriteLine("{0,12:###,##0.00}" + "{1,12:###,##0.00}" + "{2,20:###,###,##0.00}" , aDouble) 
fTotal += (double) aDouble [ 3 ]  

// 12 + 12 == 24
Console.WriteLine(space ( 24 ) + repl ( "-" , 20 ) )
// 12 + 12 + 20 == 44          
Console.WriteLine("{0,44:###,###,##0.00}" , fTotal )
                                                     

RETURN

regards
Karl-Heinz