Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda