2

I'm porting some code to Delphi XE and noticed that if I use Application.Handle to get the handle of the program, Delphi throws me an error and refuses to compile, saying:

Undeclared identifier: 'Handle'

This same behavior happens when I try to call Application.ProcessMessages. I figure something must have gotten shifted around that wasn't listed in the Unicode Migration guide.

Where did the functions and variables for 'Application' go?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Daisetsu
  • 4,846
  • 11
  • 50
  • 70

1 Answers1

12

My psychic debugging powers tell me that this unit imports SvcMgr after it imports Forms and so the Application variable in SvcMgr takes the one that you want in Forms out of scope. Or perhaps the culprit is WebBroker or CtlPanel.

You can work out which it is by CTRL clicking on the Application variable at the point of the first error and you'll land in a unit that isn't Forms.

The solution is just to reorder your imports so that Forms comes in after the others.

On the other hand, my psychic debugging powers could be broken today!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    Reminds me of `Windows.Bitmap` hiding `Graphics.Bitmap` if you mess up the `uses` order. – CodesInChaos Feb 22 '11 at 21:12
  • @CodeInChaos Too true. Only it's really `Windows.BITMAP` yelling at you, "Don't use me! I'm the wrong one!". This is one area of the language that I feel is weak. Uses was done much better in Modula-2. – David Heffernan Feb 22 '11 at 21:15
  • David, you were pretty much spot on. The problem wasn't the USES clause, but rather that I had a 'with WebBrowser1 do begin ... Application.handle ... end'. It was using the 'Application' from the webbrowser object. THANKS! – Daisetsu Feb 22 '11 at 21:16
  • @Daisetsu @CodeInChaos Thanks for letting us know. This is another area of the language which is weak. I have banned the use of `with` in my company! – David Heffernan Feb 22 '11 at 21:19
  • 1
    Maybe Embarcadero could add a Warning or Hint if a WITH causes ambiguity (to a person, obviously not to the compiler) – Gerry Coll Feb 22 '11 at 23:20
  • @Gerry That would be a great addition. I could see it being useful in all scoping resolution situations. – David Heffernan Feb 22 '11 at 23:21
  • @Gerry: I'd rather just have a "warn on with" option. Unfortunately it would throw so many warnings on the RTL/VCL that it would be difficult to use properly (or maybe that would teach people not to include VCL source in their library path, who knows) –  Feb 22 '11 at 23:43
  • @moz "include VCL source in their library path" Yak! Do people do that? Why? – David Heffernan Feb 22 '11 at 23:49
  • A warning would be best, possibly off by default. If people insist on compiling VCL code, the warning could be suppresed with {$WARN AMBIGOUS_WITH OFF} in each VCL/RTL file. – Gerry Coll Feb 23 '11 at 00:01
  • 1
    @Gerry Why just `with`? Ambiguous scoping occurs with implicit `Self.`, local variables in nested procedures, uses clauses and probably many other situations. Maybe what we really need is namespaces! – David Heffernan Feb 23 '11 at 00:03
  • @David Heffernan: if I knew that I would... actually, no I probably wouldn't be any happier, but at least I'd be more informed. I'm currently removing $(DELPHI)\Lib\Debug and other similar things from project library paths in my new job. –  Feb 23 '11 at 01:00
  • I love ambiguous and overloaded names that are right there in the VCL. And I love it when you hoist yourself on your own petard (WITH statements) even more. :-) – Warren P Feb 23 '11 at 12:04
  • With people importing gigantic units like windows willy-nilly, that will only generate heaps of alarms. Note that the scope is never ambiguous, it is always perfectly in the importing order you specified ;-) – Marco van de Voort Feb 23 '11 at 12:09
  • @Marco OK, ambiguous is the wrong word from that perspective. I agree that back-fitting something like this into existing code bases would flood you with messages. – David Heffernan Feb 23 '11 at 12:14