I’m starting with something simple by creating a string list that runs via dictionary (InfiniteStringList) and would like to be able to access the index with box brackets. The code below will explain more
This is the class (exists in a namespace)
public class InfiniteStringList{
public Dictionary<int, string> database = new Dictionary<int, string>();
//This makes foreach work
public IEnumerator GetEnumerator(){
int count = 0;
while (count < database.Count) {
yield return database [count];
count = count + 1;
}
}
//this adds to Infinite string list
public void Add(string item){
database.Add (database.Count, item);
}
}
Here is how I’m trying to access it
void Start(){
InfiniteStringList testString = new InfiniteStringList();
testString.Add ("This Is Item 1");
testString.Add ("This Is Item 2");
Debug.Log (testString [0]);
}
Here is the error I get in Unity:
Assets/testScript.cs(12,28): error CS0021: Cannot apply indexing with [ ] to an expression of type `InfiniteStringList’
How can I make “Debug.Log(testString[0])” work?
Thanks