Solved

Testing passing function

Posted on 2002-05-11
10
167 Views
Last Modified: 2010-04-04
I have callback function and want to make sure that Func only called when it is not nil and make it default to nil.

type
  TCallBack = procedure of object

procedure TBlabla.Loop(Func: TCallBack = nil);
begin
  if Func <> nil then Func;
end;
0
Comment
Question by:comicboy
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Expert Comment

by:mikepj
Comment Utility
"procedure of object" -> "procedure"
"func <> nil" -> "@func<>nil"

0
 
LVL 4

Expert Comment

by:nestorua
Comment Utility
HI,
What about: if Assigned(Func) then Func.
Sincerely,
Nestorua.
0
 
LVL 2

Expert Comment

by:mikepj
Comment Utility
Assigned() is the same as <>nil
0
 
LVL 4

Expert Comment

by:nestorua
Comment Utility
But more DELPHIable and not the same when TFunc has
parameters, if I am not mistaken.
0
 
LVL 2

Expert Comment

by:mikepj
Comment Utility
Myself I prefer to use Assigned().  From the Delphi help file:

"Assigned(P) corresponds to the test P<> nil for a pointer variable, and @P <> nil for a procedural variable."
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:nestorua
Comment Utility
That's what I tried to say. Agree with you, mikepj.
0
 

Author Comment

by:comicboy
Comment Utility
Hi, I'm newbie in Delphi could you please tell me what example of a "ponter variable" and what is "procedural variable" actually in program ?

Anyway both of you expert in this field who should I give the points ?
0
 
LVL 2

Accepted Solution

by:
mikepj earned 50 total points
Comment Utility
Hi ComicBoy,

The full answer is below.  I have provided the answer you need.  Using a pointer to a procedure is a rather special possibility which I use only rarely (mostly for sorting routines to provide a function which performs the evaluation to decide whether item x should go before or after item y.  A pointer points at something.  Usually a pointer points at a variable but it could point at other things (like a procedure).

Since it sounds like you're a beginner, I would suggest that you think of other ways you might do what you have asked.  It's just that it's a bit advanced and can possibly cause you some frustration if you don't fully understand all the concepts involved.

If you need further help with this, ask away.  It'd be good if you are using pointers and such for you to do some reading about them.  Have fun; very powerful stuff!

MP

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TCallBack = procedure;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Loop(Func: TCallBack = nil);
  end;

var
  Form1: TForm1;

procedure f;

implementation

{$R *.DFM}

procedure f;
begin
  Showmessage('hi');
end;

procedure TForm1.Loop(Func: TCallBack = nil);
begin
 if (@Func <> nil) then Func;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // let's test it
  Loop(nil);
  Loop(@f);
end;

end.
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20122794.html
http://www.experts-exchange.com/questions/Q.20122796.html
http://www.experts-exchange.com/questions/Q.20138191.html
http://www.experts-exchange.com/questions/Q.20150902.html
http://www.experts-exchange.com/questions/Q.20245947.html
http://www.experts-exchange.com/questions/Q.20245954.html
http://www.experts-exchange.com/questions/Q.20245949.html
http://www.experts-exchange.com/questions/Q.20299515.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20125957.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
 

Author Comment

by:comicboy
Comment Utility
So sorry for long delay on grading, I'm away awhile from internet reach...

Anyway Thanks alot for your great answer of massive knowledge :) you are a great expert :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now