- Home /
List of Interface classes
Hello all! I am having the biggest issue doing something so simple.
I'm getting the error: 'MageClass' is a type, which is not valid in the given context
It is my understanding that interfaces can do this, but any help would be appreciated!
I have a simple interface
script:
public interface ICharacterJob
{
string className { get; }
string classDescription { get; }
float baseHealth { get; }
float baseSpeed { get; }
}
and 2 classes that inherit from that. EX:
public class MageClass : ICharacterJob
{
//Character Job Implementation
public string className { get { return "Mage"; } }
public string classDescription { get { return "Test Description For Mage"; } }
public float baseHealth { get { return 50; } }
public float baseSpeed { get { return 17; } }
}
All I am trying to do is make a list of the two classes.
using System.Collections.Generic;
public static class CharacterClasses
{
public static List<ICharacterJob> SelectableClasses = new List<ICharacterJob>() { MageClass };
}
While creating the list you gave to pass the instances of $$anonymous$$ageClass and not the class itself
Answer by Bunny83 · Apr 08, 2020 at 03:59 AM
Well this last part:
new List<ICharacterJob>() { MageClass };
doesn't make much sense. MageClass is a type and you try to store it as value. You probably want to create an instance of that type so you have to do
new List<ICharacterJob>() { new MageClass() };
Note that "classes" can not be referenced as they are not objects. Only objects can be referenced. When you create an instance of your class you create a new object of that type. There are ways to reference type information through reflection. Every type has a "descriptor object". The type of the descriptor object is System.Type. You can get the descriptor by using the "typeof()" operator. Note that the descriptor object can only be used with reflection and reflection methods. It is not a reference to a type which as I already mentioned does not exist.
Technically generic type parameters can be seen as "references to types" but those can not be stored anywhere. Generic type parameters are more like place holders / alias names.
I actually figured this out about 5 $$anonymous$$utes ago and was so mad at myself! Don't code at 3am by the way!