mercredi 18 juillet 2018

C# Loose coupling to access to a property value

I want to create a loosely coupled classes (ClassA and ClassB) and be able to access to the value of the property "PlayerScore" (that's implemented in ClassA) from ClassB (without knowing about ClassA).

public class ClassA : IClass
{
    int myScore = 520;

    public int PlayerScore
    {
        get
        {
            return myScore;
        }
    }
}



public interface IClass
{
    int PlayerScore { get; }
}



public class ClassB
{
    //There is any way to access here to the value of "PlayerScore" without creating an instance
    //of ClassA and without depending in any way of the ClassA (like IClass myClass = new ClassA())?
}

PS: I don't know if using interfaces is the right way to do it or if there is another way.

Any idea how I can achieve this? Thanks

Aucun commentaire:

Enregistrer un commentaire