C
C#9mo 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
Angius9mo ago
foreach (T characterStat) Or better yet, just use var Ah, wait You also cast to U, huh
Zeke
ZekeOP9mo ago
Yeah I have two potential stats inheriting from CharacterStat Oh I messed up, T should be StatAttackRange (sorry!)
Angius
Angius9mo 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
ZekeOP9mo ago
I updated my image sorry T and U are two separate inherited types And I save them as fields as their types
Zeke
ZekeOP9mo ago
No description
Angius
Angius9mo ago
Yeah, but do you pass both to the method?
Zeke
ZekeOP9mo 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
Angius9mo 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
ZekeOP9mo 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
Angius9mo 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
ZekeOP9mo ago
Fair enough. Appreciate the insight
Want results from more Discord servers?
Add your server