Casting in beta 8

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Casting in beta 8

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Casting in beta 8

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am

Casting in beta 8

Post 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
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Casting in beta 8

Post by robert »

Karl-Heinz Rauscher wrote: But let's wait, until Robert jumps in .
I clicked [Submit] a few seconds before you :-)

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply