Question

I created an empty unit to store my public procedures and functions but I get access violation in the first line of a procedure, but this for the first time only I call this procedure, later calls work normally !!

The unit code:

unit myFunc;

interface

uses
  Windows, Messages, SysUtils, System.UITypes, Variants, Classes, Graphics, Controls, Forms;

  Procedure GSet(grid_form: TForm; app, operation: string);

implementation

Procedure GSet(grid_form: TForm; app, operation: string);
var
grd_idx: integer;
begin
  if operation = 'save' then begin  // Access violation in this line 

    if (grid_form.components[grd_idx] is TMyComponent) then
    (grid_form.components[grd_idx] as TMyComponent).Storedata('c:\'+app);
  end;
end;

Update: I call this procedure from form create event like this

GridSettings(myform, 'cost', 'save');
Was it helpful?

Solution

From the code in the question, the most obvious mistake is that the variable grd_idx is not initialised. You must initialise variables before using them.

On top of that it is possible that you are passing an invalid object reference in the first parameter. We cannot tell from here. However, it is possible that you are passing a global variable that has not been initialised. It looks awfully like you should be passing Self rather than that global variable.

GridSettings(Self, 'cost', 'save');

Of course, I am assuming here that Self is what you intend to pass here. Since you did not supply very much code, we can only guess as to the details of your code.

You ask how to make the OnCreate event fire after all controls are created. Well, that is already the case. The OnCreate method is fired after the controls are created and their properties streamed in.

I would also comment that using strings to select discrete options is not a good idea. Use enumerated types for that.

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