Blake Steel
Blake Steel

Reputation: 378

C# Updating an object when an object in its constructor changes

I'm not sure how to ask this question in an elegant fashion, so here. I have this class that takes in an instance of a class AbilityScores in its constructor. I am wondering if there is some way to make it so that if the instance of AbilityScores that was passed in is updated it will update the instance of Skills automatically, or even better if it doesn't have to update the instance of Skills and just updates the property abilityScores in Skills.

It probably has something to do with referencing it instead of copying it in like I'm doing here, but I'm not sure. Any help is appreciated.

class Skills
{
    AbilityScores abilityScores;
    List<SkillChecks> skillProficiencies;
    List<SkillChecks> skillExpertices;
    LevelAndLevelRelatedBonuses level;

    public Skills(AbilityScores abilityScores, List<SkillChecks> skillProficiencies, List<SkillChecks> skillExpertices, LevelAndLevelRelatedBonuses level)
    {
        this.abilityScores = abilityScores;
        this.skillProficiencies = skillProficiencies;
        this.skillExpertices = skillExpertices;
        this.level = level;
    }

    //Insert other methods here (not important at the moment)
}

Upvotes: 1

Views: 1581

Answers (1)

steve v
steve v

Reputation: 3540

What you are asking for is how object references work by default, assuming AbilityScores is a class. Your Skills class has a reference to that AbilityScores instance. If something else has that reference and acts upon the class, since you have the same reference to it, it will be updated.

From c# documentation on reference types:

there are two kinds of types in C#: reference types and value types. Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable. With value types, each variable has its own copy of the data, and it is not possible for operations on one variable to affect the other (except in the case of ref and out parameter variables, see ref (C# Reference) and out parameter modifier (C# Reference)).

The following keywords are used to declare reference types: class interface delegate

Assuming something strange isn't going on, this should happen by default. See this dotnet fiddle

using System;

public class Program
{
    public static void Main()
    {
        var scores = new AbilityScores() { Speed = 1, Durability = 1 };
        var skills = new Skills(scores);

        Console.WriteLine("Before change: " + skills.GetCurrentScores());

        scores.Speed = 2;
        scores.Durability = 0;

        Console.WriteLine("After change: " + skills.GetCurrentScores());

    }

}

public class Skills
{
    public AbilityScores abilityScores;

    public Skills(AbilityScores abilityScores)
    {
        this.abilityScores = abilityScores;
    }

    public string GetCurrentScores()
    {
        return "Speed: " + abilityScores.Speed + ", Durability: " + abilityScores.Durability;
    }


}

public class AbilityScores
{
    public int Speed {get; set;}
    public int Durability {get; set;}
}

Output:

Before change: Speed: 1, Durability: 1
After change: Speed: 2, Durability: 0

Upvotes: 1

Related Questions