C#C
C#2y ago
Zeke

Help with Generics

Here is my current code:


StatAttackRange/StatAttackDamage inherit from CharacterStat
private StatAttackRange _attackRange;

public void SetCharacterStats(List<CharacterStat> characterStats)
        {
            foreach (CharacterStat characterStat in characterStats)
            {
                if (characterStat.GetType() == _attackRange.GetType())
                {
                    _attackRange = (StatAttackRange)characterStat;
                }else if (characterStat.GetType() == _attackDamage.GetType())
                {
                    _attackDamage = (StatAttackDamage)characterStat;
                }
            }
        }


I feel like it's inelegant, and I believe the solution is to use generics. But when I try to make a generic function (see below) I'm doing something wrong and I'm not sure what.

https://i.imgur.com/tTW44Y9.png
Imgur
Was this page helpful?