Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can NIL be used for a Sender object?

Recently I was asked to automate a little sub-routine that presented a series of data records and any two of four potential buttons for the user to select after seeing an analysis of the record. The boss said having the users see the analysis was wasting time since the users invariably selected the number one choice in the button list and he was prepared to live with my guesses for all but the best of his users. So, he wanted a NEW series of buttons added to offer Handle Automatically, Handle Manually and Handle Case by Case. The last button would just run the already existing code. The second button would essentially do nothing and just exit. The first button? Well, that was the rub.

What I decided to do was to use a couple of flags and then have the automatic path just simulate the click of whatever sub-button was best, based on the analysis. The issue was that calling Button1Click(Sender) wasn't possible because the procedure running the Analysis was called RunAnalysis and wasn't attached to a specific object to pass the TObject through. I eventually refactored the guts of the Button1Click method into Button1Pressed and then called THAT from Button1Click. Thus I was able to call Button1Pressed from within RunAnalysis.

The avoided path would have been to call Button1Click(Nil). I didn't try it since I had an easy solution (Thanks Modelmaker, by the way). But my question is, would the nil reference have worked or would it have caused a disaster. Could I have called a higher function (randomly?) that did have a sender, JUST to have a sender object in the procedure call? Just how important IS the Sender object, if I don't use anything that actually REFERENCES the Sender?

System details: Delphi 7 in Win 7 programming environment for use in Windows XP.

Thanks in advance for any wisdom, GM

like image 628
GM Mugford Avatar asked Nov 30 '22 15:11

GM Mugford


1 Answers

I tend to put the event handler's code into another method when possible:

procedure TForm1.DoSomething(const Test: Boolean);
begin
  // Do Something based on Test
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DoSomething(False); // init here
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  DoSomething(TCheckBox(Sender).Checked);
end;

So when I have a need to call CheckBox1Click(nil) it's a good sign for me to pull off the code from the event handler into a separate method.

like image 64
kobik Avatar answered Dec 09 '22 13:12

kobik