Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Include some lines of code using compiler statement

Posted on 2009-05-17
15
Medium Priority
?
199 Views
Last Modified: 2012-05-07
title say all

just

{$  do5Lines of code }    ??
0
Comment
Question by:BdLm
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 12

Expert Comment

by:Hypo
ID: 24405780
Is this what you are after?

{$DEFINE do5LinesOfCode} // Remove this line if you don't want 5 lines of code...

{$IFDEF do5LinesOfCode}
  // your lines of code...
{$ENDIF}
0
 
LVL 8

Author Comment

by:BdLm
ID: 24405811
can I pass a Parameter to my lines of code ?
0
 
LVL 8

Author Comment

by:BdLm
ID: 24405816
The waek point id idef ...  I need these point 2000 x in my code   (profiler)
therfore only want I line for a piease of code to insert ...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Hypo
ID: 24405837
ok... so you want it like a macro then... I know one way to do that is using Include-files... You can then type  {$I 'C:\MyCode.pas'} and the compiler will replace all those statement with the lines that are in that file. I'll have a look if there is any other ways aswell that don't require you to put the lines in a separate file.

for instance, MyCode.Pas could contain the following line:
TButton(Sender).Caption := 'Test';

and then if you have a button event like this:
procedure TForm1.Button1Click(Sender: TObject);
begin
  {$I 'C:\MyCode.pas'}
end;

The code will compile and the text on the button will change to Test when pressed.

regards
Hypo
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24405897
I've looked around some but didn't find any other way to do this, so to me it seems that the include files are the way to do what you want. Some extra info: You don't have to quote the filename if it doesn't contain spaces in it, and the compiler assumes that the default extension for includes are .pas, so {$I My5LinesOfCode} is equivalent to {$I 'My5LinesOfCode.pas'}.

Just by curiosity, may I ask why you want to include the lines using a compilerdirective? It isn't possible for you to put the code you want to execute in a simple function?

/Hypo
0
 
LVL 31

Assisted Solution

by:moorhouselondon
moorhouselondon earned 400 total points
ID: 24406905
Trouble with using this technique is that you are going to end up with two versions of the program with different maps.  I think you are concerned about speed - putting the test code into a procedure is better if speed is not crucial.  

You can specify a parameter string when calling the program in "test" mode and use the paramstr(1) function to retrieve it.  If running in normal mode, leave the parameter blank.

http://www.delphibasics.co.uk/RTL.asp?Name=ParamStr

Doing this, you can setup Delphi to always use this "debug" string when you run the program from the IDE.  At the start of your prog have debugflag set depending on whether paramstr contains the correct keyword, and then test for this on entry to your test procedure.  If it is false, exit the test procedure immediately.
0
 
LVL 8

Author Comment

by:BdLm
ID: 24407311
{ifdef PROFILER}

   {$I 'C:\InitProfiler.pas'}
{endif}

     testCode

{$ifdef PROFILER}
 
        {$I 'C:\StopProfiler.pas'}
 {endif}

any better / shorter way in the source code ?





0
 
LVL 8

Author Comment

by:BdLm
ID: 24407327
PS:  I know http://www.prodelphi.de/indexpd.htm   but not invented here  :-)  
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24407373
var
  debugflag:boolean;


procedure profiler(whocalledme:string);
begin
if debugflag then
  begin
 
  logging routine storing date time and whocalledme

  end;
end;


procedure main;
begin
debugflag:=(paramstr(1)='mysecretdebugswitch');
profiler('program started');
.
.
.

0
 
LVL 8

Author Comment

by:BdLm
ID: 24407387
here comes a sample with prodelphi , they include the lines of code in the $ifdef statement ...



procedure TForm1.Button3Click(Sender: TObject);
begin
{$IFDEF PROFILE}asm DW 310FH; call Profint.ProfStop; end; Try; asm mov edx,79 or $99EA0000; mov eax,self; call Profint.ProfEnter; mov ecx,eax; DW 310FH; add[ecx].0,eax; adc[ecx].4,edx; end; {$ENDIF}
 
     ACheck.Filename := edit2.Text;
 
     ACheck.Name  := ' Bla Bla';
     
     ACheck.StoreResults;
{$IFDEF PROFILE}finally; asm DW 310FH; mov ecx,79; call Profint.ProfExit; mov ecx,eax; DW 310FH; add[ecx].0,eax; adc[ecx].4,edx; end; end; {$ENDIF}
end;

Open in new window

0
 
LVL 12

Accepted Solution

by:
Hypo earned 1600 total points
ID: 24407397
Yeah, you can put the defines in the inc-file as well...

procedure TForm1.Button1Click(Sender: TObject);
begin
  {$i C:\initProfiler}
  ...
  {$i C:\stopProfiler}
end;


And then in C:\InitProfiler.pas you have...
{$IFDEF PROFILER}
  // your code...
  TButton(Sender).Caption := 'Testing';
{$ENDIF}

And similar in StopProfiler.pas...
0
 
LVL 8

Author Comment

by:BdLm
ID: 24407399
@ Hypo :  just working on that solution,   :-))
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24407423
Nice, I just tried it, putting those lines you have at the start and at the end in two seperate files, compiles... well I had to remove some of your asm-code since i don't have all the global variables you have defined... but I tried it with other code, so it should work with your code as well... :)
0
 
LVL 8

Author Comment

by:BdLm
ID: 24407574
Btw:  did you see my new question , because now I need to init the profiler  var in the begin end block of my unit    http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24416082.html
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24416694
when you use the profiler, it parses all your units and adds the entry and exit procs to the beginning resp end of each function or procedure.
Why do you have to do this manually ?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question