C#switch语句中的变量范围

时间:2013-09-18 17:49:34

标签: c# scope

我有一个以此开头的函数:

    void RecordLoadPosition()
    {
        OdbcCommand command;
        if (_hasPlantGenie)
        {
            command = new OdbcCommand();
            string query;
            switch (ItemType)
            {
                case ItemTypeEnum.COIL:
                    // We're only going to add records to the coils_pg table when the coil gets moved.  After
                    // a record is added, we'll update it.
                   command = new OdbcCommand("select * from coils_pg where coil_id = " + _itemName, _db);                       

当我编译它时,我在if块的第一行没有出错,但是我得到错误抱怨我在case块中声明之前不能使用“command”。我不明白为什么函数顶部的声明在case块内不可用。

但是好的。如果它在case块中不可见,我可以声明它。我将case块中的第一个语句更改为“OdbcCommand command ...”。现在我收到一个错误,抱怨我无法声明已经在父块中声明的变量!我不能以任何方式获胜!

这里发生了什么?

当然,我可以使用不同的OdbcCommand对象,这就是我现在要做的,但我想了解这一点。

=============================================

我的原始代码示例中似乎缺少某些内容,但我不知道是什么。这是一个应该显示相同错误的小函数,但没有:

    void ScopeTest()
    {
        OdbcCommand command = null;
        if (_hasPlantGenie)
        {
            command = new OdbcCommand();
            switch (ItemType)
            {
                case ItemTypeEnum.COIL:
                    // We're only going to add records to the coils_pg table when the coil gets moved.  After
                    // a record is added, we'll update it.
                    command = new OdbcCommand();
                    break;
            }
        }
    }

============================================== < / p>

并且,因为人们要求它,这里是完整的原始函数(包括在if块顶部额外创建一个OdbcCommand对象,仅仅是为了证明它不会抛出错误:

    void RecordLoadPosition()
    {
        OdbcCommand command = null;
        if (_hasPlantGenie)
        {
            command = new OdbcCommand();
            string query;
            switch (ItemType)
            {
                case ItemTypeEnum.COIL:
                    // We're only going to add records to the coils_pg table when the coil gets moved.  After
                    // a record is added, we'll update it.
                    command = new OdbcCommand("select * from coils_pg where coil_id = " + _itemName, _db);
                    OdbcDataReader reader = command.ExecuteReader();
                    if (reader.Read())
                    {
                        query = "update plant_genie.coils_pg set x_coordinate = " +
                                XPosCurrent.ToString() +
                                ", y_coordinate = " +
                                YPosCurrent.ToString() +
                                " where coil_id = '" +
                                _itemName + "'";
                        reader.Close();
                        command.CommandText = query;
                        command.ExecuteNonQuery();
                    }
                    else
                    {
                        query = "insert into plant_genie.coils_pg(coil_id, x_coordinate, y_coordinate) values (" +
                                XPosCurrent.ToString() + YPosCurrent.ToString() +
                                "'" + _itemName + "')";
                        reader.Close();
                        command.CommandText = query;
                        command.ExecuteNonQuery();
                    }
                    break;
                case ItemTypeEnum.INNER_COVER:
                    // The inner_cover_pg table will be pre-built.  We can assume that it has records for
                    // each inner cover.
                    query = "select set_inner_cover_down(" +
                            XPosCurrent.ToString() +
                            ", " +
                            YPosCurrent.ToString() + 
                            ", '" +
                            _itemName +
                            "')";
                    OdbcCommand command = new OdbcCommand(query, _db);
                    command.ExecuteNonQuery();
                    // See if the cover has been set down in a storage location.  If it has
                    break;
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

case ItemTypeEnum.INNER_COVER:条件中,您有:

OdbcCommand command = new OdbcCommand(query, _db);

你要重新声明它,当你应该像在case ItemTypeEnum.COIL:分支一样分配它时。用这个替换该行:

command = new OdbcCommand(query, _db);