[英]How can i store some specific data from my methods
我的程序中有一些方法,其中一些被称为其他方法。我的问题是我想使用某个方法在先前方法中生成的某些数据,而我不知道该怎么做。
namespace MyProg
{
public partial class MyProg: Form
{
public static void method1(string text)
{
//procedures
method2("some text");
// Here i want to use the value from string letter from method2.
}
public static void method2(string text)
{
//procedures
string letter = "A"; //Its not A its based on another method.
}
}
}
只需使用返回值:
public partial class MyProg: Form
{
public static void method1(string text)
{
string letter = method2("some text");
// Here i want to use the value from string letter from method2.
}
public static string method2(string text)
{
string letter = "A"; //Its not A its based on another method.
return letter;
}
}
方法可以将值返回给调用方。 如果返回类型(在方法名称之前列出的类型)不为空,则该方法可以使用return关键字返回值。 带有关键字return且后跟匹配返回类型的值的语句将把该值返回给方法调用者...
由于您已经提到不能使用返回值,因此另一个选择是使用out
参数 。
public static void method1(string text)
{
string letter;
method2("some text", out letter);
// now letter is "A"
}
public static void method2(string text, out string letter)
{
// ...
letter = "A";
}
您可以将值存储在类的成员变量中(在这种情况下,该变量必须是静态的,因为引用它的方法是静态的),也可以从method2返回该值,然后从要在method1内部调用method2。用它。
我将由您自己决定如何进行编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.