开发者

When I try to SET an attribute from another class to another it doesn't allow me to

开发者 https://www.devze.com 2022-12-15 20:34 出处:网络
Here\'s the class: namespace TomeOfNewerth_WPF_ { class Hero { public string faction; public string name; public HeroType 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 clas开发者_StackOverflow中文版s, 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.


x.herotype = HeroType.Agility; is normally the code to set it. You will need to move HeroType 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号