在表单之间传递数据 - Ajax比较

时间:2021-11-01 10:03:37

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.

我之前提出的问题看起来像这样,人们不理解。我将尝试更简洁,并将使用与Web应用程序中使用的Ajax进行比较。

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).

我有主要形式。在那里,我有一个按钮,它将从一个字段中提取数据并发送到第二个窗体,一个窗口将弹出并以更有条理的方式显示数据(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).

我想知道是否有办法在SecondForm.Show上发送这些数据作为参数,如SecondForm.Show(数据)。

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.

与Ajax的比较是,当您从html页面向服务器进行Ajax调用时,您发送封装数据,服务器接收它并使用它。

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

我希望我的主表单发送数据,第二种表单接收数据并使用它。

Is it possible? How?

可能吗?怎么样?

3 个解决方案

#1


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.

确定将数据从Main发送到SecondFrom的形式。例如在TStringList中。填写主窗体上的striglist,将其用作SecondForm.Show中的参数。

#2


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.

不过,欢迎您编写其他方法,如果您愿意,可以将其称为Show。将其定义为接受您需要的任何参数,并在准备好后,可以调用继承的零参数Show方法来显示表单。

procedure TSecondForm.Show(AParam1: Integer; const AParam2: string);
begin
  // Use parameters here.
  inherited Show;
end;

#3


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.

使用OOP样式很难将所有变异限制为构造函数。很少(如果有的话)图书馆承认这种编码风格。但是,你进入构造函数的突变越多,后来被发现的风险就越低。

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.

实际上,对于您的场景,我怀疑这些风险很小。然而,作为一般原则,构造期间的初始化是合理的,并且有理由遵循该原理。

#1


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.

确定将数据从Main发送到SecondFrom的形式。例如在TStringList中。填写主窗体上的striglist,将其用作SecondForm.Show中的参数。

#2


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.

不过,欢迎您编写其他方法,如果您愿意,可以将其称为Show。将其定义为接受您需要的任何参数,并在准备好后,可以调用继承的零参数Show方法来显示表单。

procedure TSecondForm.Show(AParam1: Integer; const AParam2: string);
begin
  // Use parameters here.
  inherited Show;
end;

#3


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.

使用OOP样式很难将所有变异限制为构造函数。很少(如果有的话)图书馆承认这种编码风格。但是,你进入构造函数的突变越多,后来被发现的风险就越低。

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.

实际上,对于您的场景,我怀疑这些风险很小。然而,作为一般原则,构造期间的初始化是合理的,并且有理由遵循该原理。