【发布时间】:2020-06-16 11:30:34
【问题描述】:
[在此处输入图片描述][1]
我如何才能访问(例如,注释 Console.WriteLine())函数内的变量? 试图在主函数中声明它(在 2 个注释位置),但它说它未使用。 谢谢!
public void Form1_Load_1(object sender, EventArgs e)
{
//string filename = “ “;
try
{
//string filename = “ “;
rename("02-66977V2", "X");
//Console.WriteLine(filename);
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ToString());
}
finally
{
}
}
public void rename(string Number, string Type)
{
string filename = " ";
try
{
if (Number.Contains("V2"))
{
filename = Number + Type;
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ToString());
}
finally
{
}
}
【问题讨论】:
-
你不能(你可以,但是通过全局变量或参数,而不是“函数到函数”)。您需要研究“范围”。
-
在你的函数
rename中将rename签名更改为public string rename和return filename。您的实际问题可能是“如何获得函数的结果”。 -
谢谢大家,我会看看前面的问题。