c#中设置变量的问题

本文关键字:问题 变量 设置 | 更新日期: 2023-09-27 18:15:04

我在变量FaxPro, EmailPro, FaxStatEmailStat上得到错误。

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

错误:

名称"FaxPro"在当前上下文中不存在C:…'Form2.cs

EmailProFaxStatEmailStat等等

c#中设置变量的问题

在函数的开头声明字符串,这样它们在整个过程中都有作用域。目前,您在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();
}

变量声明超出了作用域。它们在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(); 
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
        {
            //...
            //...