【问题标题】:Why does this code compile in one solution but not in another? [closed]为什么这段代码在一个解决方案中编译而不在另一个解决方案中编译? [关闭]
【发布时间】:2014-07-15 17:08:18
【问题描述】:

我在一个解决方案中有此代码,它工作正常:

        private readonly List<Department> departments = new List<Department>();

        . . .

        private void LoadDepartments(string serialNum)
        {
            string dbContext = HandheldServerUtils.GetDBContextForSerialNum(serialNum);
            string connStr =
                string.Format(
                    @"Provider=Microsoft.ACE.OLEDB.12.0;User ID=NRBQSP;Password=NRBQSP;Data Source=C:\CCRWin\DATA\CCRDAT{0}.MDB;Jet OLEDB:System database=C:\CCRWin\Data\sscs.mdw",
                    dbContext);
            using (var conn = new OleDbConnection(connStr))
            {
                using (OleDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText =
                        @"SELECT t_accounts.account_no as AccountID, IIF(ISNULL(t_accounts.name),'[blank]',t_accounts.name) AS Name 
                        FROM t_accounts 
                        INNER JOIN td_department_accounts ON (t_accounts.account_no = 
td_department_accounts.account_no) where type = 'DE'";
                    cmd.CommandType = CommandType.Text;
                    conn.Open();
                    int i = 1;
                    using (OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
                    {
                        while (oleDbD8aReader != null && oleDbD8aReader.Read())
                        {
                            string accountNum = oleDbD8aReader.GetString(0);
                            string deptName = oleDbD8aReader.GetString(1);
                            Add(new Department {Id = i, AccountId = accountNum, Name = deptName});
                            i++;
                        }
                    }
                }
            }
        }

我在另一个代码中有非常相似的代码:

    public IEnumerable<Department> GetDepartments()
    {
        private readonly List<Department> departments = new List<Department>();
        string dbContext = "42"; 
        string connStr =
            string.Format(
                @"Provider=Microsoft.ACE.OLEDB.12.0;User ID=NRBQSP;Password=NRBQSP;Data Source=C:\CCRWin\DATA\CCRDAT{0}.MDB;Jet OLEDB:System database=C:\CCRWin\Data\sscs.mdw",
                dbContext);
        using (var conn = new OleDbConnection(connStr))
        {
            using (OleDbCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText =
                    @"SELECT t_accounts.account_no as AccountID, IIF(ISNULL(t_accounts.name),'[blank]',t_accounts.name) AS Name 
                    FROM t_accounts 
                    INNER JOIN td_department_accounts ON (t_accounts.account_no = td_department_accounts.account_no) where type = 'DE'";
                cmd.CommandType = CommandType.Text;
                conn.Open();
                int i = 1;
                using (OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
                {
                    while (oleDbD8aReader != null && oleDbD8aReader.Read())
                    {
                        string accountNum = oleDbD8aReader.GetString(0);
                        string deptName = oleDbD8aReader.GetString(1);
                        departments.Add(new Department { Id = i, AccountId = oleDbD8aReader.GetInt16(0), Name = oleDbD8aReader.GetString(1) });
                        i++;
                    }
                }                    
            }
        }
        return departments;
    }

...它咳出 26 个错误,例如:

";预期"

在这条线和其他线上:

using (var conn = new OleDbConnection(connStr))

-and "} expected" 就在开头的 "{"

--and “命名空间不能直接包含字段或方法等成员”

在这一行:

departments.Add(new Department { Id = i, AccountId = oleDbD8aReader.GetInt16(0), Name = oleDbD8aReader.GetString(1) });

-以及“类、结构或接口成员声明中的无效标记 '!='”:

while (oleDbD8aReader != null && oleDbD8aReader.Read())

...以及“(”和“,”和“;”和“{”和“=”和“使用”的许多其他“无效令牌”错误

-和两个“类型或命名空间定义,或预期的文件结尾

如果我注释掉除 return 语句之外的所有代码,那么它就是:

public IEnumerable<Department> GetDepartments()
{
    return null;
}

...它编译得很好。

为什么这里对鹅好对鹅不好?

【问题讨论】:

  • 所以你说你在一个项目中尝试了一些代码,然后你在另一个项目中尝试了 不同 代码,你想知道为什么后者没有编译?你的逻辑有问题。
  • 看起来您在某处忘记了 }{。没有完整的代码很难指出。
  • 在第二种情况下,您的方法中有一个 private 变量。这是非法的。
  • @derpirscher:这是完整的代码。
  • @B.ClayShannon 我对此表示怀疑。既没有类也没有命名空间定义。

标签: c# ienumerable oledbconnection oledbcommand oledbdatareader


【解决方案1】:

这一行……​​

private readonly List<Department> departments = new List<Department>();

... 不应在函数中,而应在类范围之外,或者将其设为 var

【讨论】:

  • 令人惊讶的是,像这样的疏忽会让人联想到无数嘶嘶作响的眼镜蛇,威胁性地恶意地编织,而只需要一只友好的猫鼬挥舞着“删除这里的私人名称”标志。跨度>
  • 如果用户在下面的代码中添加它,它不应该是“只读”。
  • @KoBE 改变分配给只读字段的对象是完全合法的。您只是无法重新分配该字段,仅此而已。
  • @dcastro 是这样吗?谢谢(你的)信息。我想我撤回了我之前的声明。我会进一步调查。谢谢!
猜你喜欢
  • 2021-05-20
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
相关资源
最近更新 更多