-1

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

    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 块中声明它之前我不能使用“命令”。我不明白为什么函数顶部的声明在 case 块中不可用。

但是没问题。如果它在 case 块中不可见,我可以声明它。我将案例块中的第一条语句更改为“OdbcCommand 命令...”。现在我收到一个错误,抱怨我不能声明已经在父块中声明的变量!我怎么都赢不了!

这里发生了什么?

当然,我可以只使用不同的 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;
            }
        }
    }

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

而且,因为人们要求它,这里是完整的原始函数(包括在 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;
            }
        }
    }
4

1 回答 1

3

在你的case ItemTypeEnum.INNER_COVER:情况下,你有:

OdbcCommand command = new OdbcCommand(query, _db);

当您应该像在case ItemTypeEnum.COIL:分支中一样分配它时,您正在重新声明它。将该行替换为:

command = new OdbcCommand(query, _db);
于 2013-09-18T18:10:32.727 回答