xsharp.eu • Casting in beta 8
Page 1 of 1

Casting in beta 8

Posted: Sat Jan 12, 2019 8:32 am
by wriedmann
Hi,

the following fragment of code does not compiles anymore in Beta 8:

Code: Select all

tvItem.state := INDEXTOSTATEIMAGEMASK( ( word ( _cast , fCheck ) ) + 1 )
The entire function is the following:

Code: Select all

function TreeView_SetCheckState( hwndTreeView as ptr, hItem as ptr, fCheck as logic ) as logic pascal
local tvItem 		is _winTV_ITEM
local lSet 			as logic

if _and ( GetWindowLong( hwndTreeView , GWL_STYLE ), TVS_CHECKBOXES ) == TVS_CHECKBOXES
  tvItem.mask := _or ( TVIF_HANDLE , TVIF_STATE )
  tvItem.hItem := hItem
  tvItem.stateMask := TVIS_STATEIMAGEMASK
  tvItem.state := INDEXTOSTATEIMAGEMASK( ( word ( _cast , fCheck ) ) + 1 )
  lSet := TreeView_SetItem( hwndTreeView, @tvItem )
 else
   lSet				:= false
 endif

 return lSet
I have changed that to

Code: Select all

if fCheck
  tvItem.state := INDEXTOSTATEIMAGEMASK( word( 2 ) )
else
  tvItem.state := INDEXTOSTATEIMAGEMASK( word ( 1 ) )
endif
Of course I have applied this change also to the VO version of my code.
Wolfgang

Casting in beta 8

Posted: Sat Jan 12, 2019 11:06 am
by robert
Wolfgang,
This is exactly one of the cases where the cast is dangerous.
The word is 2 bytes, but what is the size of the logical fCheck ?
In VO the logic is represented as 32 bits, but who knows what the value is. Is a logic represented with all bits set to 1 or only one bit set to one? That makes a huge difference. If all bits are set to 1 then the word(_CAST, fCheck) will return 2^15. Otherwise it will return 1 (because intel is littel endian and the one is stored in the lower 2 bytes).

In .Net the logic is 8 bits. Casting 8 bits to a 16 bit value, what do you expect.
Your solution seems right to me.

Robert

Casting in beta 8

Posted: Sat Jan 12, 2019 11:07 am
by Karl-Heinz
Hi Wolfgang,

This seems to work:

Code: Select all

LOCAL fCheck AS LOGIC
LOCAL w AS WORD  

	fCheck := TRUE 


 
        //     INDEXTOSTATEIMAGEMASK( ( word ( _cast , fCheck ) ) + 1 )			
	
	INDEXTOSTATEIMAGEMASK(  (WORD) System.Convert.changeType  ( fCheck , typeof(WORD)  )  + 1 )
	
		 
        //  -------
    	
	w := (WORD) System.Convert.changeType  ( fCheck , typeof(WORD)  ) + 1
	
			    
	IF fCheck
		
		? "True" , w
		
	ELSE 
		?"False" , w 
		
	ENDIF 		

But let's wait, until Robert jumps in .

regards
Karl-Heinz

Casting in beta 8

Posted: Sat Jan 12, 2019 11:19 am
by robert
Karl-Heinz Rauscher wrote: But let's wait, until Robert jumps in .
I clicked [Submit] a few seconds before you :-)

Robert