C# 如何将同一文件中的变量用于另一段代码

C# 如何将同一文件中的变量用于另一段代码,c#,C#,我正在编写一个小代码,现在已经遇到了一个错误 string getCode(string code) { int count = code.Length; if (count % 2 == 0) { string error = "no"; } else { string error = "yes"; string error_message = "There is something wrong w

我正在编写一个小代码,现在已经遇到了一个错误

string getCode(string code)
{
    int count = code.Length;
    if (count % 2 == 0)
    {
        string error = "no";
    }
    else
    {
        string error = "yes";
        string error_message = "There is something wrong with the code. Program.cs - line 23";
    }

    if (error == "yes")
        return null;

    return error;
}
现在我在if语句中设置变量
error
。但是如果我以后说
if(error==“yes”)
不起作用。它告诉我错误
当前内容中不存在名称“error”
我现在已经在网上搜索了15-20分钟。尝试许多修复和示例。使用全局变量,甚至尝试为我的变量创建另一个类并从中读取变量。这是可行的,但是我不能再设置变量了。或者我就是不知道怎么做。你能帮帮我吗?我会非常感激的


谢谢

如果,您应该声明
错误
变量超出
的范围,如下所示:

string error = null;
if (count % 2 == 0)
{
    error = "no";
}
else
{
    error = "yes";
    string error_message = "There is something wrong with the code. Program.cs - line 23";
}

if (error == "yes")
    return null;
public static class Globals
{
    public static String error = "yes"; 
}
这样,您可以在
if
块或
else
块中访问此变量,或者更一般地说,在方法
getCode
范围中包含的所有范围中访问此变量


旁注:
error\u message
只能在
else
的块中使用,您只需声明一个变量并为其赋值。您可能还必须将此声明移到
错误声明旁边
您应该将
错误
变量声明在
if
的范围之外,如下所示:

string error = null;
if (count % 2 == 0)
{
    error = "no";
}
else
{
    error = "yes";
    string error_message = "There is something wrong with the code. Program.cs - line 23";
}

if (error == "yes")
    return null;
public static class Globals
{
    public static String error = "yes"; 
}
这样,您可以在
if
块或
else
块中访问此变量,或者更一般地说,在方法
getCode
范围中包含的所有范围中访问此变量


旁注:
error\u message
只能在
else
的块中使用,您只需声明一个变量并为其赋值。您可能还必须将此声明移到
错误声明旁边

您必须将变量声明在整个代码可以访问的范围之外, 像


您必须将变量声明在整个代码可以访问的范围之外, 像


您可以通过创建一个类,然后在需要使用该类来创建一个如下所示的类时调用该类,从而使变量成为全局变量:

string error = null;
if (count % 2 == 0)
{
    error = "no";
}
else
{
    error = "yes";
    string error_message = "There is something wrong with the code. Program.cs - line 23";
}

if (error == "yes")
    return null;
public static class Globals
{
    public static String error = "yes"; 
}
当需要调用它时,您可以添加更多变量

global.error

您可以通过创建一个类,然后在需要使用该类来创建一个如下所示的类时调用该类,从而使变量成为全局变量:

string error = null;
if (count % 2 == 0)
{
    error = "no";
}
else
{
    error = "yes";
    string error_message = "There is something wrong with the code. Program.cs - line 23";
}

if (error == "yes")
    return null;
public static class Globals
{
    public static String error = "yes"; 
}
当需要调用它时,您可以添加更多变量

global.error
将代码更改为:

string getCode(string code)
        {
            int count = code.Length;
            string error = "";
            if (count % 2 == 0)
            {
                error = "no";
            }
            else
            {
                error = "yes";
                string error_message = "There is something wrong with the code. Program.cs - line 23";
            }

            if (error == "yes")
            return null;
        }
您需要知道的是,如果您在一个范围内声明一个变量,那么它只在该范围内可见。 大括号定义了一个作用域,这样错误变量在if-true分支和else分支中都是可见的,这两个分支是两个不同的变量,在不同的作用域中具有相同的名称

更改的代码将错误变量声明移动到包含方法范围(getCode)中,使其可访问。

将代码更改为:

string getCode(string code)
        {
            int count = code.Length;
            string error = "";
            if (count % 2 == 0)
            {
                error = "no";
            }
            else
            {
                error = "yes";
                string error_message = "There is something wrong with the code. Program.cs - line 23";
            }

            if (error == "yes")
            return null;
        }
        string getCode(string code)
        {
            int count = code.Length;
            string error = null;
            if (count % 2 == 0)
            {
                error = "no";
            }
            else
            {
                error = "yes";                    
            }

            if (error == "yes")
                return "something that you intended to return when there is an error";
            else
                return "something that you intended to return when there is no error";
        }
您需要知道的是,如果您在一个范围内声明一个变量,那么它只在该范围内可见。 大括号定义了一个作用域,这样错误变量在if-true分支和else分支中都是可见的,这两个分支是两个不同的变量,在不同的作用域中具有相同的名称



更改后的代码将错误变量声明移动到包含方法范围(getCode)中,使其可访问。

C或C#?那是两种截然不同的语言!
error
的范围必须在
if
之前,并且与
int count
的级别相同。此外,这不是C,因此不应被标记为C。@Someprogrammerdude的C#@DragonThinks不起作用。.声明变量的范围仅限于封闭的大括号
{}
。也就是说,您的代码声明了两个不同的
error
变量。一个在
if(){…}
中,另一个在
else{…}
中。在
if else
.C或C#之后的作用域中没有
error
变量?那是两种截然不同的语言!
error
的范围必须在
if
之前,并且与
int count
的级别相同。此外,这不是C,因此不应被标记为C。@Someprogrammerdude的C#@DragonThinks不起作用。.声明变量的范围仅限于封闭的大括号
{}
。也就是说,您的代码声明了两个不同的
error
变量。一个在
if(){…}
中,另一个在
else{…}
中。在
if else
之后的作用域中没有
error
变量。这不会改变任何事情,它只会给出另一个错误。。。请参见屏幕截图@20Host.nl声明变量时,不能在任何封闭范围内重新声明。简单地说,当您在
getCode
方法体中声明变量
error
时,您不能在
if
块或
for
块等语句中重新声明它。您必须从一开始就为error赋值,否则你会得到错误:使用未分配的局部变量'error',我个人建议你阅读以下内容,以提高你对变量范围的理解。这不会改变任何事情,它只会给出另一个错误。。。请参见屏幕截图@20Host.nl声明变量时,不能在任何封闭范围内重新声明。简单地说,当您在
getCode
方法体中声明变量
error
时,您不能在
if
块或
for
块等语句中重新声明它。您必须从一开始就为error赋值,否则你会得到错误:使用未分配的局部变量'error',我个人建议你阅读以下内容,以提高你对变量范围的理解。现在,
if(error==“yes”)
不再给出任何错误。但现在剩下的是。。。看截图仔细阅读我的代码。您必须删除声明
        string getCode(string code)
        {
            int count = code.Length;
            string error = null;
            if (count % 2 == 0)
            {
                error = "no";
            }
            else
            {
                error = "yes";                    
            }

            if (error == "yes")
                return "something that you intended to return when there is an error";
            else
                return "something that you intended to return when there is no error";
        }