.net is a extremly dangerous thing ;-)

This forum is meant for anything you would like to share with other visitors
User avatar
Chris
Posts: 4904
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

.net is a extremly dangerous thing ;-)

Post 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
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

.net is a extremly dangerous thing ;-)

Post 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
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

.net is a extremly dangerous thing ;-)

Post 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
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

.net is a extremly dangerous thing ;-)

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

.net is a extremly dangerous thing ;-)

Post 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
Post Reply