views:

129

answers:

1

Have 6 forms, 1 Base and 5 inherited.The Base has the following snippet:

procedure TMechan.Open1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Form1.Memo1.Lines.LoadFromFile(OpenDialog1.FileName );
  CopyCylMemoToRecord;
  ShowMechanicalValues;
end;

Since this snippet is in the Base it's also inherited by 5 others. Problem is: When executing OpenDialog the Base is overwritten instead of the inherited form. HELP

+10  A: 

I'm not exactly sure what you mean with the Base is overwritten but your use of Form1 in your code should probably be changed to Self.

Form1 is a global reference to one instance of (probably) your Base form. You could create a zillion TMechan form instances where each and every Open1Click would be changing a property of that One Form.

I assume the forms are autocreated. While that is easy to get going, I would strongly advice you to not autocreate each and every form and to remove all global references to forms (like this form1) where possible.

The only form that should get autocreated is your main form. All other forms should be created when needed, something like myform := TMechan.Create(AOwner);

procedure TMechan.Open1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Self.Memo1.Lines.LoadFromFile(OpenDialog1.FileName );
  CopyCylMemoToRecord;
  ShowMechanicalValues;
end;
Lieven
JFTR: 99.999% of the time "Self" in front of a dot is superfluous, i.e. `Self.Something(SomeParameters);` is the same as `Something(SomeParameters);`.
Ulrich Gerhardt
@Ulrich Gerhardt - true but after spending some time programming c# and using tools like FxCop encouraging the use of `this`, I find myself writing `Self` more often to increase readability. That offcourse is *very* subjective.
Lieven