Question

Using Delphi XE5 I'd like to use Application variable (TApplication type) in other unit's class. If I try to pass it with var or out parameter and then use it - I get Access Violation error. Is there a way to get data from Application in shared .pas library?

  for TempComponent in Application.MainForm do
      if (TempComponent is TTextControl) then
         ShowMessage('Text Assigned : ' + TempComponent.Name)
      else
         ShowMessage('No TTextControl Descendant : ' + TempComponent.Name);

I have included FMX.Forms in every unit. The above code works in main form class procedures, but when I use it in other unit I get AV in running application.

Was it helpful?

Solution

You state that this code:

for TempComponent in Application.MainForm do
  if (TempComponent is TTextControl) then
     ShowMessage('Text Assigned : ' + TempComponent.Name)
  else
     ShowMessage('No TTextControl Descendant : ' + TempComponent.Name);

results in an access violation. The only reasonable explanation for that is that either:

  1. Application is nil, or
  2. Application.MainForm is nil.

The former seems a little unlikely since you'd have to be running the code before the FMX.Forms unit intialized (or after it finalized). So the likely explanation is that you are running the code before the main form has been created, or you are running the code in a program that does not have a main form.

When you encounter access violation errors, you can debug them yourself to understand. What you need to do is to set a breakpoint on the line that causes the exception. When it triggers, inspect the variables used by that line of code. Usually you will find that one of the variables is nil.

OTHER TIPS

Application is a global variable defined in FMX.Forms. So as long as you add FMX.Forms to your uses statement, you can use the Application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top