Tuesday, November 30, 2004

Getting Rid of Delphi Warnings

Someone asked how to get rid of the Delphi warning:

"Unsafe typecast of 'Pointer' to 'TObject'"

The purpose of the "Unsafe" warnings is to let you know your code will not be easily compatible with .NET (or other safe platforms). If your target platform is only going to be Win32 then you can use the compiler directives:

{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_CAST OFF}
Also worth noting are the platform warnings. These were originally to let you know if your code was going to be compatible with Linux. So if you are only targeting Win32 (or any specific platform) you can disable these warnings with:
{$WARN SYMBOL_PLATFORM OFF}

These are also accessible via the Project / Options / Compiler Messages screen. Generally you should only disable a warning if you know it does not apply to your specific program. I should hope no-one ever disables all hits and warnings.

If you want to know the compiler directive version of the warning then just look in the project .cfg file for the -w lines added when you enable or disable a warning. Then just append {$WARN in front of it and OFF|ON} after it for most of the warnings.

2 comments:

Anonymous said...

The problem isn’t solved with removing the warnings – merely hidden. More interesting would be to know how to e.g. modify the TTreeNode so that the .Data pointer became a TObject (something about inline inheritance and such)

unused said...

I agree, it is always better to "solve" the warning when possible. But anytime you are using a pointer you will get the unsafe warning. You could wrap it in a class or component, but then that wrapper would get the warning when it compiled.