Bruie
Bruie

Reputation: 1235

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 GameSpeed within another class. Can you please help with this as I am a new developer and can't figure it out.

thank you

Upvotes: 1

Views: 110

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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;
    }
}

Upvotes: 4

Related Questions