Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Refactoring help required

Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America asked on
Delphi
14 Comments1 Solution230 ViewsLast Modified:
I'm going through some refactoring and found this type of thing {SNIPPET 1} in our application. I have approx 25 of
these taking up around 400 or 500 lines of code.

I'd like to pare it down to ONE procedure, like {SNIPPET 2}, for all reports but can't figure out
how to handle the different form classes. This would reduce my code to <100 lines.


BTW, using D6...
{SNIPPET 1}
procedure TMainProgram.Summary1Click(Sender: TObject);
var
  TheScreen: TPREmployRpt;
  RecVar:    TCrystalData;
begin
  InitializeCrystalABRecord(RecVar);
  theScreen := TPREmployRpt.create(application);
  RecVar.theReport := 'PRREP01B';
  theScreen.ReptRec := RecVar;
  theScreen.ShowModal;
  theScreen.free;
end;
 
procedure TMainProgram.Detail1Click(Sender: TObject);
var
  TheScreen: TPREmployRpt;
  RecVar:    TCrystalData;
begin
  InitializeCrystalABRecord(RecVar);
  theScreen := TPREmployRpt.create(application);
  RecVar.theReport := 'PRREP01A';
  theScreen.ReptRec := RecVar;
  theScreen.ShowModal;
  theScreen.free;
end;
 
procedure TMainProgram.Employee1Click(Sender: TObject);
var
  TheScreen: TprReport3;
  RecVar:    TCrystalData;
begin
  InitializeCrystalABRecord(RecVar);
  theScreen := TprReport3.create(application);
  RecVar.theReport := 'PRREP04A';
  theScreen.ReptRec := RecVar;
  theScreen.ShowModal;
  theScreen.free;
end;
 
 
{SNIPPET 2}
procedure TMainProgram.DoReport(AReportClass: String; AReport: String);
var
  ReportFormClass: TFormClass;
  RecVar:          TCrystalData;
begin
  InitializeCrystalABRecord(RecVar);
  try
    ReportFormClass := TFormClass(FindClass(AReportClass));
    // Not sure how to handle this line below so I can access
    // the ReptRec property of the form.
    theScreen       := ReportFormClass.Create(Application);
    try
      RecVar.theReport  := AReport;
      theScreen.ReptRec := RecVar;
      theScreen.ShowModal;
    finally
      theScreen.free;
    end;
  except
  end;
end;
 
procedure TMainProgram.OpenPOBalances1Click(Sender: TObject);
begin
  DoReport('TPoOpenBalReport', 'POREP01');
end;
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert GFlag of Belgium imageOracle dba
Commented:
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answers