C# 名称“..."在当前上下文中不存在 [英] C# The name ' ... ' doesn't exist in the current context

查看:82
本文介绍了C# 名称“..."在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C# 新手,我尝试从最基础的知识中学习它,但我坚持学习课程.我做了我的第一个例子来练习它工作正常但是当我增加一点复杂性时我得到一个错误:

I'm new in C# and I try to learn it from the very basics, but I stuck with classes. I made my first example to practice which is working properly but when I add a little complexity I get an error:

当前上下文中不存在名称‘iArcher’."

"The name 'iArcher' doesn't exist in the current context."

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

Please help to explain what's wrong and suggest a proper (and easy) solution.

谢谢!

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# 有作用域.作用域内的项可以看到包含它的作用域中的所有内容,但外部作用域无法在内部作用域内看到.您可以在此处阅读有关范围的信息.

C# has scopes. An item within a scope can see everything in the scopes that contain it, but outer scopes can't see within inner scopes. You can read about scopes here.

举个例子:

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

iArcher 在您的 if 语句的范围内,因此 if 语句之外的代码无法看到它.

iArcher is in the scope of your if statement, so code outside that if statement can't see it.

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

To resolve this, move the definition or iArcher outside the if statement:

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".


Note that this now leaves you with another problem: input cannot be both "create: archer" and "property: archer".

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

One solution might be to move reading user input inside a loop, while keeping iArcher outside that loop:

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"作为输入.

To exit the loop simply type "exit" as the input.

这篇关于C# 名称“..."在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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