-8

I have this code in one solution, and it works fine:

        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++;
                        }
                    }
                }
            }
        }

I have this very similar code in another:

    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;
    }

...and it coughs up 26 Errors, such as:

"; expected"

on this line and others:

using (var conn = new OleDbConnection(connStr))

-and "} expected" right after the opening "{"

--and "A namespace cannot directly contain members such as fields or methods"

on this line:

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

-and "Invalid token '!=' in class, struct, or interface member declaration" on this line:

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

...as well as many other "invalid token" errors for "(" and "," and ";" and "{" and "=" and "using"

-and two "Type or namespace definition, or end-of-file expected"

If I comment out all the code except a return statement so that it is just:

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

...it compiles fine.

Why is what is good for the goose not good for the gander here?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

4

This line ...

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

... should not be in the function, but outside, at the class scope, or make it a var.

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
JacksonHaenchen
  • 1,437
  • 1
  • 11
  • 16
  • It's amazing how an oversight like that can conjure up myriads of hissing cobras, weaving menacingly and maliciously, when nothing more was needed than a friendly mongoose waving a "remove the private designation here" sign. – B. Clay Shannon-B. Crow Raven Jul 15 '14 at 17:19
  • It shouldn't be 'readonly' if the user is adding to it in the code below. – Cory Jul 15 '14 at 17:19
  • 2
    @KoBE It's perfectly legal to mutate an object assigned to a readonly field. You just can't reassign the field, that's all. – dcastro Jul 15 '14 at 17:26
  • @dcastro Is that so? Thanks for the info. I guess I recant my statement from earlier. I'll look into it further. Thanks! – Cory Jul 15 '14 at 17:29