开发者

Accessing a Property

I have created the below object:

public class GameProperties : ModelBase
{
    private int _gameSpeed;
    public virtual int GameSpeed
    {
        get { return _gameSpeed; }
        set
        {
            _gameSpeed = value;
            OnPropertyChanged("GameSpeed");
        }
    }

    public void SetGameSpeed(int speed)
    {
        _gameSpeed = speed;
    }   

}

I set the GameSpeed property when loading my application:

GameProperties newProperty = new GameProperties(); newProperty.SetGameSpeed(4);

what i want to do is now reference the GameSp开发者_C百科eed within another class. Can you please help with this as I am a new developer and can't figure it out.

thank you


You could pass the GameProperties reference to the other class either in the constructor or the method you are calling. For example:

public class SomeOtherClass
{
    public void Foo(GameProperties properties)
    {
        int speed = properties.GameSpeed;
    }
}

or in the constructor:

public class SomeOtherClass
{
    private readonly GameProperties _properties;
    public SomeOtherClass(GameProperties properties)
    {
        _properties = properties;
    }

    public void Foo()
    {
        int speed = _properties.GameSpeed;
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜