2011-07-29 39 views
0

enter image description here变量不当前环境的和无效的论据存在

我想验证一个文本box.I已经验证了几个其他的文本框和他们的工作fine.This其中有一定的误差。 我的代码看起来正确的me.Someone请您指​​出我的错误,告诉我为什么Visual Studio 2010的提示是无效的参数和变量的错误不在当前环境:

+3

阅读上如何使用'out':http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.80).aspx –

+1

您还没有在addTextBox_Validating方法中声明errorMsg。你的代码不正确,它只是错误的。 –

+0

你应该首先声明出变量... –

回答

3

您需要addTextBox_Validating功能定义string errorMsg;打电话之前ValidAddress

0

您还没有宣布ERRORMSG字符串。

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

} 

在ValidAddress中,ERRORMSG字符串到函数作为参数传递,这样就不会出现这个问题。

1

你需要用它作为out参数之前定义的变量ERRORMSG。

string errorMsg; 
-1

哪里errorMsg定义?它看起来像它在发送作为参数ValidAddress,所以addTextBox_Validating,是一个不同的方法完全不能够访问它,因为errorMsg的作用范围仅在ValidAddress存在。长话短说,你还没有初始化你的变量。

1

Read up on how to use out

虽然作为out参数传递变量,无需之前被传递初始化,需要调用方法该方法返回到之前分配一个值。

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

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

尝试通过添加声明它

例如改变addTextBox_Validating

var errorMsg = string.Empty; 

if (!ValidAddress(... 

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

心连心
艾伦。

1

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

串ERRORMSG = NULL;

为addTextBox_Validating()的第一行