Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当前上下文中不存在变量,参数无效_C#_Visual Studio 2010 - Fatal编程技术网

C# 当前上下文中不存在变量,参数无效

C# 当前上下文中不存在变量,参数无效,c#,visual-studio-2010,C#,Visual Studio 2010,我正在尝试验证一个文本框。我已经验证了其他几个文本框,它们工作正常。这一个有一些错误。 我的代码看起来是正确的。请有人指出我的错误,并告诉我为什么Visual Studio 2010会提示一个无效参数和变量错误,而不是当前上下文中的错误:您需要定义字符串errorMsg需要定义string errorMsg中,您尚未声明errorMsg字符串 private void addTextBox_Validating (object sender, CancelEventArgs e) { s

我正在尝试验证一个文本框。我已经验证了其他几个文本框,它们工作正常。这一个有一些错误。
我的代码看起来是正确的。请有人指出我的错误,并告诉我为什么Visual Studio 2010会提示一个无效参数和变量错误,而不是当前上下文中的错误:

您需要定义
字符串errorMsgValidAddress
之前,在
addTextBox\u验证
函数中的code>需要定义
string errorMsgValidAddress
之前,在
addTextBox\u验证
函数中的code>中,您尚未声明errorMsg字符串

private void addTextBox_Validating (object sender, CancelEventArgs e)
{
    string errorMsg = "";
    ...etc

}

在ValidAddress中,errorMsg字符串作为参数传递给函数,因此不会出现此问题。

您尚未声明errorMsg字符串

private void addTextBox_Validating (object sender, CancelEventArgs e)
{
    string errorMsg = "";
    ...etc

}

在ValidAddress中,errorMsg字符串作为参数传入函数,因此不会出现此问题。

您需要先定义errorMsg变量,然后再将其用作输出参数

string errorMsg;

在将errorMsg变量用作out参数之前,需要先定义它

string errorMsg;

哪里定义了
errorMsg
?它看起来像是作为参数发送到
ValidAddress
,因此
addTextBox\u验证
,作为一种完全不同的方法,无法访问它,因为
errorMsg
的作用域仅存在于
ValidAddress
中。长话短说,您还没有初始化变量。

哪里定义了
errorMsg
?它看起来像是作为参数发送到
ValidAddress
,因此
addTextBox\u验证
,作为一种完全不同的方法,无法访问它,因为
errorMsg
的作用域仅存在于
ValidAddress
中。长话短说,您还没有初始化变量。

虽然作为输出参数传递的变量在传递之前不需要初始化,但调用方法需要在方法返回之前分配一个值

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

虽然作为输出参数传递的变量在传递之前不需要初始化,但调用方法需要在方法返回之前分配一个值

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

据我所知,errorMsg没有在任何地方声明

尝试通过添加声明来更改addTextBox\u

e、 g

out变量需要在其使用的上下文中声明

hth

艾伦。

据我所知,errorMsg没有在任何地方声明

尝试通过添加声明来更改addTextBox\u

e、 g

out变量需要在其使用的上下文中声明

hth

Alan。

您需要将字符串作为第二个参数传递给ValidAddress。尝试添加

字符串errorMsg=null


作为addTextBox_Validating()的第一行,您需要将字符串作为第二个参数传递给ValidAddress。尝试添加

字符串errorMsg=null


作为addTextBox_Validating()的第一行,请阅读如何使用
out
:您尚未在addTextBox_Validating方法中声明errorMsg。您的代码不正确,只是完全错误。您应该首先声明out变量…阅读如何使用
out
:您没有在addTextBox\u验证方法中声明errorMsg。您的代码不正确,只是明显错误。您应该首先声明out变量。。。