Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Why is use of specifc enum invalid in generic method with enum as the type

Why does this line of code (context below)

if (spellEnum == Spell.Fireball) return "Fire ball";

produce the error:

'Spell' is a type parameter, which is not valid in the given context

public abstract class DataSet : ScriptableObject
{
    public abstract string DisplayName<T>(T arg) where T : Enum;
}

public enum Spell { Fireball, Icestorm, MagicMissile };

public class SpellDataSet : DataSet
{
    public override string DisplayName<Spell>(Spell spellEnum)
    {
        if (spellEnum == Spell.Fireball) return "Fire ball";

        return "Other spell";
    }
}

public class DataLibrary
{
     void main()
    {
        SpellDataSet spellDataSet = new SpellDataSet();
        strint test = spellDataSet.DisplayName(Spell.Fireball);
    }
}

I want a generic way to access data about various types (spells, items, characters), with data about specific kinds of that type (Spell1, Spell2, etc) accessed via that type's respective enum (Spell), and a universal method (defined in abstract class) across all types will produce the user-friendly name (DisplayName).

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I still don't understand why the original code produces an error, but I have achieved what I wanted via making the abstract class generic, rather than only the DisplayName method. Code that works:

public abstract class DataSet<T> : ScriptableObject where T: Enum
{
    public abstract string DisplayName(T arg);
}

public enum Spell { Fireball, Icestorm, MagicMissile };

public class SpellDataSet : DataSet<Spell>
{
    public override string DisplayName(Spell spellEnum)
    {
        if (spellEnum == Spell.Fireball) return "Fire ball";

        return "Other spell";
    }
}

public class DataLibrary
{
    void main()
    {
        SpellDataSet spellDataSet = new SpellDataSet();
        string test = spellDataSet.DisplayName(Spell.Fireball);
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!