How can I update a property's value after changing it's input value in C#?

Viewed 68

I have this interface IHasCooldown with these properties:

int Id { get; }
float CooldownDuration { get; } 

In another class (let's call Attack), I set the Id as the atkId and the CooldownDuration as attackCooldown. The attackCooldown being a property so it can be changed.

public float attackCooldown { get; set; }
int atkId = 2;
public int Id => atkId;
public float CooldownDuration => attackCooldown;

Finally, in class SubAttack, I set the attackCooldown to 10. But since CooldownDuration is set at the start, any change afterwards isn't computed.

Also, a lambda operator is a bit hairy for me yet.. Got this part from a tutorial.

I'd love to hear some ideas on how to tackle this!

The cooldown system that I got is from here, but since it doesn't account for variable times, I'm tweaking it a little bit. https://www.youtube.com/watch?v=2zGvt4NnIYw

2 Answers

According to your description, the code should be:

class Program
{
    static void Main(string[] args)
    {
        var subAttack = new SubAttack();
        Console.WriteLine("Please Input CooldownDuration:");
        var input = int.Parse(Console.ReadLine());
        subAttack.PrintAttackCooldown();
        subAttack.PrintAttackCooldown(input);
        Console.ReadKey();
    }


}

interface IHasCooldown
{
    int Id { get; }
    float CooldownDuration { get; }
}

class Attack : IHasCooldown
{
    public float attackCooldown { get; set; }
    int atkId = 2;
    public int Id => atkId;
    public float CooldownDuration => attackCooldown;
}

class SubAttack : Attack
{
    public void PrintAttackCooldown()
    {
        Console.WriteLine($"CooldownDuration is {CooldownDuration}");
    }

    public void PrintAttackCooldown(int cooldown)
    {
        attackCooldown = cooldown;
        Console.WriteLine($"You Input CooldownDuration is {CooldownDuration}");
    }
}

The output is:

Please Input CooldownDuration:
9
CooldownDuration is 0
You Input CooldownDuration is 9

Therefore, setting attackCooldown on SubAttack should be effective, and CooldownDuration will calculate the latest value each time it is used.

By the way:

public float CooldownDuration => attackCooldown equals public float CooldownDuration { get { return attackCooldown; } }

Your

public float CooldownDuration => attackCooldown;

Is a Property. Without a lambda expression you could also write it as

public float CooldownDuration
{
    get { return attackCooldown; }
}

This is not set once but rather always returns the current value of attackCooldown.


Note though that the attackCooldown since it is also public and get; set is totally redundant. You could as well make it

public float CooldownDuration { get; set; }

and use that instead. The interface only defines the minimum requirements for a Property but it is allowed to also give it a setter even though it is not required by the interface.

If only your class shall be able to write to it you can also make it

public float CooldownDuration { get; private set; }

or if also subclasses shall be allowed to set the value

public float CooldownDuration { get; protected set; }
Related