简体   繁体   English

从一个控件传递到另一个类变量的值?

[英]Value passing from one control to another class variable?

I have one textbox1 in form1, I have another class called ClassDemo() , this class does not have a form. 我在form1中有一个textbox1 ,我还有一个名为ClassDemo()类,该类没有表单。 I have one method in this class, called HelloWorld() inside hello world I have one string val variable. 我在此类中有一个方法,在hello world中称为HelloWorld()我有一个string val变量。

I want to pass the textbox1 value to ClassDemo's variable when the HelloWorld(), method is called. 我想在调用HelloWorld()方法时将textbox1的值传递给ClassDemo的变量。

How can I do that? 我怎样才能做到这一点?

You can try multiple ways here. 您可以在这里尝试多种方式。

  1. Make the method as parameterized method. 将方法设为参数化方法。 Put a string parameter inside the method declaration and then whenever you call that method, pass that string value that you want to pass. 将一个string参数放在方法声明中,然后每当调用该方法时,传递要传递的string值。
  2. Store the string value in global variable or Session(only if needed). string值存储在全局变量或会话中(仅在需要时)。 This wasy, your value can flow across the application and can be accessed in any area wherever its needed. 太浪费了,您的价值可以遍及整个应用程序,并且可以在任何需要的地方访问。
  3. Pass by Value or Pass by reference. 按值传递或按引用传递。 You can opt for either one in C# to pass variables/values across connected areas. 您可以选择使用C#中的任何一种来跨连接区域传递变量/值。

Hope this clears the idea. 希望这能清除想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM