【问题标题】:C# The name ' ... ' doesn't exist in the current contextC# 名称“...”在当前上下文中不存在
【发布时间】:2019-02-20 14:23:07
【问题描述】:

我是 C# 的新手,我尝试从基础开始学习它,但我坚持使用类。我做了我的第一个例子来练习它可以正常工作,但是当我增加一点复杂性时,我得到一个错误:

“名称‘iArcher’在当前上下文中不存在。”

请帮助解释问题所在并提出适当(且简单)的解决方案。

谢谢!

using System;

namespace Units
{
    class Archer
    {
        public int id;
        public int hp;
        public float speed;
        public float attack;
        public float defence;
        public float range;

        public void setProp(int id, int hp, float sp, float at, float de, float ra)
        {
            this.id = id;
            this.hp = hp;
            speed   = sp;
            attack  = at;
            defence = de;
            range   = ra;
        }

        public string getProp()
        {
            string str = "ID        = " + id +      "\n" +
                         "Health    = " + hp +      "\n" +
                         "Speed     = " + speed +   "\n" +
                         "Attack    = " + attack +  "\n" +
                         "Defence   = " + defence + "\n" +
                         "Range     = " + range +   "\n" ;

            return str;
        }

        static void Main(string[] args)
        {
            string input = Console.ReadLine();

            if (input == "create: archer")
            {
                Archer iArcher = new Archer();
                iArcher.setProp(100, 20, 4f, 8f, 3.5f, 25f);
            }

            if (input == "property: archer")
            {
                Console.WriteLine(iArcher.getProp());    // ERROR!
            }
            Console.ReadLine();
        }
    }
}

【问题讨论】:

标签: c# syntax-error


【解决方案1】:

C# 有作用域。范围内的项目可以看到包含它的范围内的所有内容,但外部范围无法看到内部范围内的所有内容。你可以阅读范围here

举个例子:

if (input == "create: archer")
{
    Archer iArcher = new Archer();
    iArcher.setProp(100, 20, 4f, 8f, 3.5f, 25f);
}

iArcher 在你的 if 语句的范围内,所以在 if 语句之外的代码看不到它。

要解决此问题,请将定义或 iArcher 移到 if 语句之外:

Archer iArcher = new Archer();
if (input == "create: archer")
{
    iArcher.setProp(100, 20, 4f, 8f, 3.5f, 25f);
}

if (input == "property: archer")
{
    Console.WriteLine(iArcher.getProp());
}

请注意,这会给您带来另一个问题:input 不能同时是“create:archer”和“property:archer”。

一种解决方案可能是将读取用户输入移动到循环内,同时将iArcher 保持在循环外:

Archer iArcher = new Archer();
string input = null;

while ((input = Console.ReadLine()) != "exit")
{
    if (input == "create: archer")
    {
        iArcher.setProp(100, 20, 4f, 8f, 3.5f, 25f);
    }
    else if (input == "property: archer")
    {
        Console.WriteLine(iArcher.getProp());
    }
}

要退出循环,只需键入“exit”作为输入。

【讨论】:

  • 谢谢!它工作正常。我还有一个问题:如何从其他命名空间/类访问我的 getProp() 方法?我需要在那里做一个 Archer 类的新实例吗?
  • 每次创建实例时,它都是一个单独的实例,并为其分配了自己的内存,等等。如果您需要从另一个类访问实例,则必须传递该对象。
  • 你能给我一个简单的例子吗?如何传递一个对象?
  • @Bush 与将数字传递给“setProp”的方式相同。比如public void DoStuff(Archer myArcher)调用为otherClassInstance.DoStuff(myArcher),或者通过构造函数:public OtherClass(Archer myArcher) { this._myArcher = myArcher; }实例化为var otherClassInstance = new OtherClass(myArcher);
【解决方案2】:

移动这一行:

Archer iArcher = new Archer();

在你的 if 块之外,它会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2013-10-02
    • 2014-04-22
    • 2017-06-17
    • 2015-09-11
    相关资源
    最近更新 更多