User Tools

Site Tools


casting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Last revisionBoth sides next revision
casting [2018/09/03 19:03] – created wolfgangriedmanncasting [2018/09/04 03:35] wolfgangriedmann
Line 16: Line 16:
 The first assignment will compile and work in X#, because the compiler knows the type of both variables and inserts a conversion method - so this code is safe. The first assignment will compile and work in X#, because the compiler knows the type of both variables and inserts a conversion method - so this code is safe.
 The second assignment will not compile in X# because it cannot work in a .NET world - it is simply not admitted. A member of the development team stated: ''For this reason, we intentionally disallowed that specific syntax (OBJECT(_CAST, i)) in order to trap some problems in compile time and not at runtime later.'' The second assignment will not compile in X# because it cannot work in a .NET world - it is simply not admitted. A member of the development team stated: ''For this reason, we intentionally disallowed that specific syntax (OBJECT(_CAST, i)) in order to trap some problems in compile time and not at runtime later.''
 +
 +In VO, sometimes casts are used as follows:
 +<code visualfoxpro>local p as ptr
 +local oWindow as DataWindow
 +...
 +p := @oWindow</code>
 +and then p is is assigned to an int
 +<code visualfoxpro>local n as int
 +n := p</code>
 +The variable n is then used in several ways, like sorts, callbacks, in parameters, etc. To access the object again, n is casted to the object:
 +<code visualfoxpro>oWindow := object( _cast, n )</code>
 +This works in VO, until the object has not moved by the garbage collector. When the garbage collector has moved the object, the pointer and the int variable still point to a (maybe valid) memory location, but not to the original object. Therefore this is very dangerous also in VO.
 +In .NET such pointers are not allowed, therefore the development team has disallowed the compilation of such code.
 +The only valid representation of an object by a numeric variable is when the numeric is an index to an array of objects, and it remains valid also when the garbage collector kicks in. But this type of valid code is rarely seen in VO code.
  
 Please see the entire thread about it in the X# forum: [[https://www.xsharp.info/forum/public-product/832-2-0-0-2-cannot-cast-type-int-to-object|www.xsharp.info/forum/public-product/832-2-0-0-2-cannot-cast-type-int-to-object]], specially the last two messages. Please see the entire thread about it in the X# forum: [[https://www.xsharp.info/forum/public-product/832-2-0-0-2-cannot-cast-type-int-to-object|www.xsharp.info/forum/public-product/832-2-0-0-2-cannot-cast-type-int-to-object]], specially the last two messages.
  
casting.txt · Last modified: 2018/09/07 04:04 by wolfgangriedmann