How to reference a class in an enumerator
I have two classes on an object that are a database for two different collections. I wish to use an enumerator to easily select which script is being used as the database. However doing a code like this: public enum SelectionType{ private ClassOne database;, private ClassTwo database }
I know that the two classes have the same reference, but I want to be able to use those interchangeable, so if I choose to use ClassOne
ClassTwo
won't be usable.
Answer by JedBeryll · Apr 20, 2018 at 06:05 AM
Not sure if I understand what you need so hopefully this will be useful. You can use either inheritance or interface.
Inheritance:
public class DataBase {
public abstract string[] GetWhateverValues();
}
public class DataBaseType1 : DataBase {
public string[] myStrings;
public override string[] GetWhateverValues() {
return myStrings;
}
}
public class DataBaseType2 : DataBase {
public override string[] GetWhateverValues() {
return new string[100];
}
}
Interface:
public interface IDataBase {
string[] GetWhateverValues();
}
public class DataBaseType1 : IDataBase {
public string[] myStrings;
public string[] GetWhateverValues() {
return myStrings;
}
}
public class DataBaseType2 : IDataBase {
public string[] GetWhateverValues() {
return new string[100];
}
}
In either case you can have a reference to the base class/interface type and hold the implementors in that. Example: DataBase db = new DataBaseType1();
Are you suggesting that I use two classes in one script, then reference that script and choose one? I'm not quite sure what you are getting at.
Hm maybe I misunderstood you. $$anonymous$$ind showing what you currently have?
I realized I was over-complicating things and Ins$$anonymous$$d make an If statement based off that enumerator and just used different names.