C
C#3mo 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;
}
}
}
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
11 Replies
Angius
Angius3mo ago
foreach (T characterStat) Or better yet, just use var Ah, wait You also cast to U, huh
Zeke
Zeke3mo ago
Yeah I have two potential stats inheriting from CharacterStat Oh I messed up, T should be StatAttackRange (sorry!)
Angius
Angius3mo ago
private StatAttackRange _attackRange;

public void SetCharacterStats(List<CharacterStat> characterStats)
{
foreach (CharacterStat characterStat in characterStats)
{
if (characterStats is T cs)
{
_attackRange = cs;
}else if (characterStat is U cs)
{
_attackDamage = cs;
}
}
}
private StatAttackRange _attackRange;

public void SetCharacterStats(List<CharacterStat> characterStats)
{
foreach (CharacterStat characterStat in characterStats)
{
if (characterStats is T cs)
{
_attackRange = cs;
}else if (characterStat is U cs)
{
_attackDamage = cs;
}
}
}
maybe? Ur, wait, why even have U
Zeke
Zeke3mo ago
I updated my image sorry T and U are two separate inherited types And I save them as fields as their types
Zeke
Zeke3mo ago
No description
Angius
Angius3mo ago
Yeah, but do you pass both to the method?
Zeke
Zeke3mo ago
The initial list im passing in is full of inherited stats Not base classes So I needed to throw away stats like health : CharacterStat or attackspeed : CharacterStat Sort through anyway
Angius
Angius3mo ago
Seems like
public void SetStat(List<CharacterStat> stats)
{
foreach (var stat in stats)
{
switch (stat) {
case AttackRange ar:
_attackRange = ar;
break
case AttackDamage ad:
_attackDamage = ad;
break
}
}
}
public void SetStat(List<CharacterStat> stats)
{
foreach (var stat in stats)
{
switch (stat) {
case AttackRange ar:
_attackRange = ar;
break
case AttackDamage ad:
_attackDamage = ad;
break
}
}
}
would be enough Or, well, an if and else-if instead of a switch
Zeke
Zeke3mo ago
The solution you linked is similar to my own non generic version, but I thought this was a perfect case for generics. I'm mostly trying to learn I got scolded in a class once for using "-variable- is Type" and was told "use generics you silly goose" lol
Angius
Angius3mo ago
I don't see a use case for generics here tbh It probably wouldn't be how I handle character stats in a game either tbh, but that's another thing
Zeke
Zeke3mo ago
Fair enough. Appreciate the insight