当我尝试将属性从另一个类设置到另一个类时,它不允许我
这是类:
namespace TomeOfNewerth_WPF_
{
class Hero
{
public string faction;
public string name;
public HeroType herotype;
public enum HeroType
{
Agility,
Strength,
Intelligence
}
}
}
现在在另一个类中,只是为了测试,我尝试实例化 Hero 类,并设置 Herotype 属性,如下所示:
namespace TomeOfNewerth_WPF_
{
class Spell
{
Hero x = new Hero();
public void lol()
{
x.herotype = x.; //How can I set it?
}
}
}
我从 Enum 创建 Herotype 属性的唯一原因是使应用程序更加健壮不依赖文字字符串。
感谢您的帮助。
Here's the class:
namespace TomeOfNewerth_WPF_
{
class Hero
{
public string faction;
public string name;
public HeroType herotype;
public enum HeroType
{
Agility,
Strength,
Intelligence
}
}
}
Now in another class, just for testing I'm tring to instance the Hero class, and set the herotype property, like so:
namespace TomeOfNewerth_WPF_
{
class Spell
{
Hero x = new Hero();
public void lol()
{
x.herotype = x.; //How can I set it?
}
}
}
The only reason I created the herotype property from an Enum was to make the application more robust and not rely on literal strings.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
x.herotype = HeroType.Agility;
通常是设置它的代码。您需要将HeroType
移到类之外才能使其正常工作。就其价值而言,这在构造函数中可能会更好,并且您应该考虑通过 Properties 而不是公共成员变量来公开类信息。
x.herotype = HeroType.Agility;
is normally the code to set it. You will need to moveHeroType
outside of the class for this to work.For what it's worth, this might be better off in a constructor, and you should look into exposing class information through Properties instead of public member variables.