“X"在这种情况下不存在 [英] "X" does not exist in this context

查看:30
本文介绍了“X"在这种情况下不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白第二部分中这个错误背后的逻辑.

I don't understand the logic behind this error in the second section.

名称newMove"在当前上下文中不存在.

上面不是第一节就定义了吗?

Isn't it define just above in the first section?

            //First section
            if (base.IsPlayer1 == true)
            {
                Move newMove = moveList.Detect();
            }
            else if (base.IsPlayer1 == false)
            {
                MoveKeyboard newMove = moveListKeyboard.DetectMove();
            }

            //Second section
            if (newMove != null)
            {
                 PlayMove();
            }

谢谢.

推荐答案

您看到的是范围问题.newMoveifelse if 语句中定义,并且只能在创建变量的任何块中访问.

What you're seeing is a scope problem. newMove is defined within the if or else if statement and is only accessible within whichever block created the variable.

可以在上面定义它,但看起来您有两种不同的Types(MoveMoveKeyboard).如果您可以将它们合并为同一类型,则您可以在第一个 if 语句上方使用单个定义.

You could define it above, but it looks like you have two different Types (Move and MoveKeyboard). If you can consolidate those to the same type you could have a single definition above the first if statement.

这篇关于“X"在这种情况下不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆