【发布时间】: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();
}
}
}
【问题讨论】:
-
Name does not exist in Visual Studio 或 The name '…' does not exist in the current context 的可能副本,其标题几乎完全相同...
标签: c# syntax-error