Solved

Include some lines of code using compiler statement

Posted on 2009-05-17
15
194 Views
Last Modified: 2012-05-07
title say all

just

{$  do5Lines of code }    ??
0
Comment
Question by:BdLm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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 100 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 400 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

628 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