-1

I made a question that looked like this before and people didn't understand. I'll try to be more concise and will use a comparison with Ajax used in web applications.

I have the main form. There I have one button that will extract data from a field and send to a second form, a window that will pop up and display the data in a more organized way (A TListBox).

I would like to know if there is a way to on SecondForm.Show to send this data as parameters, like SecondForm.Show(data).

The comparison to Ajax is that when you make an Ajax Call from a html page to the server, you send encapsulated data, the server receives it and works with it.

I want my main form to send the data, the second form to receive the data and use it.

Is it possible? How?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tiago Duque
  • 1,956
  • 1
  • 12
  • 31
  • What makes you think we didn't understand? http://stackoverflow.com/questions/28886991/how-to-handle-data-to-a-new-form-created-trough-button-click – David Heffernan Mar 06 '15 at 13:06
  • 1
    I'm wondering if the problem is that you've not yet learnt how to write new methods other than by getting the IDE to generate event handlers. That could explain the disconnect. – David Heffernan Mar 06 '15 at 13:28

3 Answers3

2

If I were you, I'd add parameters to the form's constructor so that it has all the information it needs from the moment it exists.

constructor TSecondFrom.Create(AOwner: TComponent; AParam1: Integer; const AParam2: string);
begin
  inherited Create(AOwner);
  // Use or store parameters here
end;

You're welcome to write some other method instead, though, and you can call it Show, if you want. Define it to accept whatever parameters you need, and when you're ready, you can call the inherited zero-argument Show method to make the form appear.

procedure TSecondForm.Show(AParam1: Integer; const AParam2: string);
begin
  // Use parameters here.
  inherited Show;
end;
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Thanks for the deep exposition of how to apply the constructor method. I have to study OOP deeper to do it correctly. For now I'll use the simple answer, but thanks a lot. – Tiago Duque Mar 06 '15 at 13:42
1

Decide in what form to send the data from Main to SecondFrom. For instance in a TStringList. Fill the striglist on the mainform, use it as parameter in SecondForm.Show.

Birger
  • 4,343
  • 21
  • 35
  • This is not the most elegant way, but is the simplest one. I'll study further the constructor/parameters way and try to implement in the future. But I'll stick to this one for now. Thanks everyone, it was very helpful. – Tiago Duque Mar 06 '15 at 13:40
  • Why filling string list on the main form and then using it as parameter? Why not go and directly fill the string list on second form instead? This way he can also have third form with compleetly different data in it. – SilverWarior Mar 06 '15 at 14:38
  • Who says the second form has a string list, @Silver? Why should the first form need to know the implementation of the second? If we just use the string list as a parameter, then the second form can do whatever it needs to with it. By passing a parameter, we reduce coupling. Nothing about this precludes having a third form. – Rob Kennedy Mar 06 '15 at 15:29
  • @RobKennedy If the first form is sending data to the second form then it should know in what format that data should be sent. So it does need to know a litle bit about second form implementation. Also what if the OP decides that he would like to send data using same approach from second form to third form? Using of your suggested approach in such scenario could lead to several problems especially if the second form gets destroyed before the third form as it would probably also destroy StringList with it. That is unless your application is using ARC. – SilverWarior Mar 07 '15 at 19:17
1

This is the answer from your other question that you deleted. It still applies I believe.

Always prefer passing state in the constructor if that is viable.

Problems with state occur when it changes. Multi-threading is confounded by mutating state. Code that makes copies of state become out of sync if the state changes later.

Objects cannot be used while the constructor is executing. That's the time to mutate state since no other party can see the effects of that mutation. Once the constructor returns the newly minted object to its new owner, it is fully initialized.

It's hard with an OOP style to limit all mutation to constructors. Few, if any, libraries admit that style of coding. However, the more mutation you push into the constructor, the lower the risk of being caught out later.

In reality, for your scenario, I suspect that these risks are minimal. However, as a general principle, initialization during construction is sound and it makes sense to adhere to the principle uniformly.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Yeah, but I have to study more of OOP not to do stuff wrong. I'll stick to simple answers for now, but your answer is very enligthening. – Tiago Duque Mar 06 '15 at 13:41