Okay so I have a class that represents my own key value pair system (I know you’ll probably LoL at it but…)
what I’m trying to do now is make it so you can access values using key references like you would with a Dictionary.
<I Am Calling This Collection A “Table”>
Example: Debug.Log(myTableVar [Key]);
Here Is The Class:
[System.Serializable]
public class TableItem <TKey, TValue>{
public TKey Key;
public TValue Value;
public TableItem(TKey Key, TValue Value){
this.Key = Key;
this.Value = Value;
}
}
[System.Serializable]
public class Table <TKey, TValue> {
static System.Type keyType = typeof(TKey);
static System.Type valueType = typeof(TValue);
static List <TKey> keys = new List<TKey> ();
static List <TValue> values = new List<TValue> ();
public IEnumerator GetEnumerator(){
for (int i =0; i < keys.Count; i++) {
yield return new TableItem<TKey,TValue> (keys[i],values[i]);
}
}
public void Add (TKey Key, TValue Value){
if (Key.GetType () == keyType && Value.GetType () == valueType) {
if(!keys.Contains (Key)){
keys.Add (Key);
values.Add (Value);
}
}
else {
Debug.LogError ("Cannot Convert: <"+Key.ToString ()+", "+Value.ToString ()+"> To: <" + keyType.ToString ()+", "+ valueType.ToString()+">.");
}
}
And here is the script that is running it:
table.Add (0, "String");//<-----This Works
TableItem <int,string> i = new TableItem<int, string> (10, "DDD");//<-----This Works
table.Add (i.Key, i.Value); //<-----This Works
foreach(TableItem<int,string> kvp in table){//<-----This Works
Debug.Log (kvp.Key + " : " + kvp.Value);
}
Debug.Log (table [0]);//<-----This Does Not Work (How Do I Make It Work???)***
Thanks In Advance!