switch语句中的C#变量范围

switch语句中的C#变量范围,c#,scope,C#,Scope,我有一个函数,它的开头是: void RecordLoadPosition() { OdbcCommand command; if (_hasPlantGenie) { command = new OdbcCommand(); string query; switch (ItemType) { case I

我有一个函数,它的开头是:

    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);                       
command = new OdbcCommand(query, _db);
当我编译它时,在if块的第一行上没有得到错误,但是我得到错误,抱怨在case块中声明“command”之前我不能使用它。我不明白为什么函数顶部的声明在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;
            }
        }
    }

在您的
案例ItemTypeEnum.内封面:
条件中,您有:

OdbcCommand command = new OdbcCommand(query, _db);
当您应该像在
case ItemTypeEnum.COIL:
分支中一样分配它时,您正在重新定义它。将该行替换为以下内容:

    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);                       
command = new OdbcCommand(query, _db);

为什么要将对象实例化为新的OdbcCommand()您将在下面的开关中何时执行此操作?您的错误描述没有意义。在分配变量时,在声明错误之前,您将无法使用。您可以粘贴更多的代码吗?我自己的一个类似示例中没有此类错误。请提供演示此问题的完整代码示例。也就是说,我的猜测是您在
if
语句结束后或在
catch
块中访问
命令,此时可能没有给它赋值,这就是您得到错误的地方。@ROBERTRICHARDSON,请再次发布此方法的完整主体,与您观察的代码完全相同错误。同时发布您看到的确切编译错误。您能发布完整的代码吗?我认为这里缺少主要问题。就是这样!非常感谢您和所有花时间帮助我解决这个问题的人!我没有想到问题的根源可能在报告错误的行之下。我不知道我想我以前见过。