Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-01

Illegal type conversion: "LongInt" to "TObject"

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

If I compile the code snippet shown below in 'mode delphi' it works fine. If I try and compile it with 'mode objfpc' it fails with a compiler error as shown here:

/home/graemeg/programming/personal/fpginstall/src/rttihelpers.pas(156,43) Error: Illegal type conversion: "LongInt" to "TObject"

-----------------------------------------
// RTTI extension that allows "property paths".
//   eg:  tiSetProperty(MyObject, 'User.Address.City', 'Cape Town');
//
procedure tiSetProperty(const AObject: TObject; const APropPath: string;
  const APropValue: Variant);
var
  LPropInfo: PPropInfo;
  LValue: Variant;
begin
  Assert(APropPath  '', 'APropPath not assigned');
  LPropInfo := tiGetPropInfo(AObject.ClassType, APropPath, @AObject);
  if Assigned(LPropInfo) and Assigned(LPropInfo^.SetProc) then
  begin
    case tiGetTypeInfo(LPropInfo)^.Kind of
      tkClass:
        SetObjectProp(AObject, LPropInfo, TObject(Integer(APropValue)));
  //                       ERROR OCCURS HERE  -------^

tkEnumeration,tkBool:
[...snip...]
-----------------------------------------

In both compiler modes, I am using the same FPC 2.4.0 compiler under a 64bit Linux system. What is the correct way to resolve this error in compiler mode 'objfpc'?

Changing the offending line as shown below fixes the compiler error, but I'm not 100% sure if that is what I'm supposed to do. I guessing here. :-(

  SetObjectProp(AObject, LPropInfo, TObject(PtrUInt(APropValue)));

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/
Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 43 lines
Hi, If I compile the code snippet shown below in 'mode delphi' it works fine. If I try and compile it with 'mode objfpc' it fails with a compiler error as shown here: /home/graemeg/programming/personal/fpginstall/src/rttihelpers.pas(156,43) Error: Illegal type conversion: "LongInt" to "TObject" ----------------------------------------- // RTTI extension that allows "property paths". // eg: tiSetProperty(MyObject, 'User.Address.City', 'Cape Town'); // procedure tiSetProperty(const AObject: TObject; const APropPath: string; const APropValue: Variant); var LPropInfo: PPropInfo; LValue: Variant; begin Assert(APropPath '', 'APropPath not assigned'); LPropInfo := tiGetPropInfo(AObject.ClassType, APropPath, @AObject); if Assigned(LPropInfo) and Assigned(LPropInfo^.SetProc) then begin case tiGetTypeInfo(LPropInfo)^.Kind of tkClass: SetObjectProp(AObject, LPropInfo, TObject(Integer(APropValue))); // ERROR OCCURS HERE -------^ tkEnumeration,tkBool: [...snip...] ----------------------------------------- In both compiler modes, I am using the same FPC 2.4.0 compiler under a 64bit Linux system. What is the correct way to resolve this error in compiler mode 'objfpc'? Changing the offending line as shown below fixes the compiler error, but I'm not 100% sure if that is what I'm supposed to do. I guessing here. :-( SetObjectProp(AObject, LPropInfo, TObject(PtrUInt(APropValue)));

This is correct _if_ APropvalue is a 64-bit integer on a 64-bit system.

Better dump the whole procedure. Using variants is inviting problems.

Michael.

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote 5 lines
> > >  SetObjectProp(AObject, LPropInfo, TObject(PtrUInt(APropValue))); This is correct _if_ APropvalue is a 64-bit integer on a 64-bit system.

In that case APropValue will be a reference to a Object. I trust reference to objects on a 64-bit system is a 64-bit integer? And on 32-bit systems they will be a 32-bit integer.

Better dump the whole procedure. Using variants is inviting problems.

It is one function that can handle various data types. To not use variants, I would have to create stacks of overloaded functions all with pretty much the same implementation - that's lots of code duplication. For now that function works 100% and has been unit tested
- so I feel pretty confident that in this case variants are doing their job correctly.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/