2011-11-18 58 views
1

我在变量FaxProEmailPro,FaxStatEmailStat上收到错误。C#中的故障设置变量

while (reader.Read()) 
{ 
    string CustNo = reader["CUSTNO"].ToString(); 
    string Phone = reader["PHONE"].ToString(); 
    string Fax = reader["FAX"].ToString(); 
    string Email = reader["PRI_EMAIL"].ToString(); 
    string Type = reader["TYPE"].ToString(); 

    if (Type.Contains("H")) 
    { 
     if (Type.Contains("F")) 
     { 
      string FaxStat = "Y"; 
      string FaxPro = "PENDING"; 
     } 
     else 
     { 
      string FaxStat = "N"; 
      string FaxPro = "NONE"; 
     } 
     if (Type.Contains("E")) 
     { 
      string EmailStat = "Y"; 
      string EmailPro = "PENDING"; 
     } 
     else 
     { 
      string EmailStat = "N"; 
      string EmailPro = "NONE"; 
     } 
//outbox 
// id, account, type, title, number, fax, email, faxpro, emailpro, faxstat, emailstat, filepath, datesent 

     MySqlCommand mycommand = new MySqlCommand("INSERT INTO outbox (id, account, type, title, number, fax, email, faxpro, emailpro, faxstat, emailstat, filepath, datesent) VALUES('0','" + CustNo + "', 'CUSTOMER', 'test', '" + Phone + "', '" + Fax + "', '" + Email + "', '" + FaxPro + "', '" + EmailPro + "', '" + FaxStat + "', '" + EmailStat + "', 'test', NOW())", conn); 
    mycommand.ExecuteNonQuery(); 

错误是:... \为Form2.cs

...等等:

'FaxPro' 这个名字不会在目前情况下存在C EmailPro,FaxStatEmailStat

+3

您的变量超出范围 - 它们只存在于它们被声明的{大括号}之间。请阅读[变量作用域](http://msdn.microsoft.com/en-us/library/aa691132%28VS.71%29.aspx) –

+0

您应该小写变量名称。 –

回答

0

变量的声明是超出范围。它们在if语句中声明。将声明移动到这样的外部范围:

while (reader.Read()) 
{ 
    string CustNo = reader["CUSTNO"].ToString(); 
    string Phone = reader["PHONE"].ToString(); 
    string Fax = reader["FAX"].ToString(); 
    string Email = reader["PRI_EMAIL"].ToString(); 
    string Type = reader["TYPE"].ToString(); 

    string FaxStat = string.Empty; 
    string FaxPro = string.Empty; 
    string EmailStat = string.Empty; 
    string EmailPro = string.Empty; 
    if (Type.Contains("H")) 
    { 
     if (Type.Contains("F")) 
     { 
      FaxStat = "Y"; 
      FaxPro = "PENDING"; 
     } 
     else 
     { 
      FaxStat = "N"; 
      FaxPro = "NONE"; 
     } 
     if (Type.Contains("E")) 
     { 
      EmailStat = "Y"; 
      EmailPro = "PENDING"; 
     } 
     else 
     { 
      EmailStat = "N"; 
      EmailPro = "NONE"; 
     } 
     //outbox 
     // id, account, type, title, number, fax, email, faxpro, emailpro, faxstat, emailstat, filepath, datesent 

     MySqlCommand mycommand = new MySqlCommand("INSERT INTO outbox (id, account, type, title, number, fax, email, faxpro, emailpro, faxstat, emailstat, filepath, datesent) VALUES('0','" + CustNo + "', 'CUSTOMER', 'test', '" + Phone + "', '" + Fax + "', '" + Email + "', '" + FaxPro + "', '" + EmailPro + "', '" + FaxStat + "', '" + EmailStat + "', 'test', NOW())", conn); 
     mycommand.ExecuteNonQuery(); 
3

在你的函数的开头声明你的字符串,所以它们的范围都在整个范围内。目前,您在if/else语句块中声明FaxPro, EmailPro, FaxStat, EmailStat,一旦该块结束,它们就会超出范围。

通过在函数开头声明它们一次,您将避免在while循环中多次声明它们。

//small example 
public void myFunc() 
{ 
    string CustNo, Phone, Fax, Email, Type, FaxStat, FaxPro, EmailStat, EmailPro; 

    //set up query and reader 
    //... 
    while(reader.read()) 
    { 
     CustNo = reader["CUSTNO"].ToString(); 
     //etc. 
    } 
    //reader.close(); conn.close(); 
} 
0
while (reader.Read()) 
{ 
    string CustNo = reader["CUSTNO"].ToString(); 
    string Phone = reader["PHONE"].ToString(); 
    string Fax = reader["FAX"].ToString(); 
    string Email = reader["PRI_EMAIL"].ToString(); 
    string Type = reader["TYPE"].ToString(); 
    // Declare your variables here 
    string FaxStat, FaxPro, EmailStat, EmailPro; 

    if (Type.Contains("H")) 
    { 
     if (Type.Contains("F")) 
     { 
      FaxStat = "Y"; 
      FaxPro = "PENDING"; 
     } 
     else 
     { 
      //... 
      //... 
+0

你不应该重新声明变量。 'string FaxState =“Y”;' –

+0

@ m-y:哎呀!谢谢,纠正。 – Otiel

+0

谢谢!我开始使用PHP。我认为这可以让你摆脱很多。但是,C#迫使我从一开始就应该学习编程。 –