在C#中设置变量的麻烦

时间:2011-11-18 22:37:50

标签: c# variables

我在变量FaxProEmailProFaxStatEmailStat上收到错误。

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();

错误是:

  

当前上下文C:...... \ Form2.cs

中不存在名称'FaxPro'

...等EmailProFaxStatEmailStat

3 个答案:

答案 0 :(得分:3)

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

通过在函数开头声明它们一次,您将避免在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();
}

答案 1 :(得分: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
        {
            //...
            //...

答案 2 :(得分: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();