Help with Generics
Here is my current code:
StatAttackRange/StatAttackDamage inherit from 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
11 Replies
foreach (T characterStat)
Or better yet, just use var
Ah, wait
You also cast to U
, huhYeah I have two potential stats inheriting from CharacterStat
Oh I messed up, T should be StatAttackRange (sorry!)
maybe?
Ur, wait, why even have
U
I updated my image sorry
T and U are two separate inherited types
And I save them as fields as their types
Yeah, but do you pass both to the method?
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
Seems like
would be enough
Or, well, an if and else-if instead of a switch
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
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
Fair enough. Appreciate the insight