Solved

Testing passing function

Posted on 2002-05-11
10
168 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
ID: 7003295
"procedure of object" -> "procedure"
"func <> nil" -> "@func<>nil"

0
 
LVL 4

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

by:mikepj
ID: 7003440
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

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

Author Comment

by:comicboy
ID: 7004007
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
ID: 7004025
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
ID: 7014935
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
ID: 7026946
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

17 Experts available now in Live!

Get 1:1 Help Now