Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中设置变量时遇到的问题#_C#_Variables - Fatal编程技术网

C# 在C语言中设置变量时遇到的问题#

C# 在C语言中设置变量时遇到的问题#,c#,variables,C#,Variables,我在变量FaxPro、EmailPro、FaxStat和EmailStat上遇到错误 while (reader.Read()) { string CustNo = reader["CUSTNO"].ToString(); string Phone = reader["PHONE"].ToString(); string Fax = reader["FAX"].ToString(); string Email = reader["PRI_EMAIL"].ToStr

我在变量
FaxPro
EmailPro
FaxStat
EmailStat
上遇到错误

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”


。。。对于
EmailPro
FaxStat
、和
EmailStat
,等,在函数的开头声明字符串,使其始终具有作用域。目前,您正在
if/else
语句块中声明
FaxPro、EmailPro、FaxStat、EmailStat
,一旦该块结束,它们就超出范围

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();
通过在函数开始时声明一次,可以避免在
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(); 

您的变量超出范围-它们只存在于声明它们的{大括号}之间。请仔细阅读你的变量名应该小写。你不应该重新声明变量<代码>字符串传真状态=“Y”谢谢!我开始使用PHP。我想这会让你逃脱很多。但是,C#迫使我从一开始就学习编程。