2011-11-18 98 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#迫使我從一開始就應該學習編程。 –