Search code examples
c#dictionarymultidimensional-arraysortedlist

Indexer for a 'SortedList' of 'List' of 'own class'


I've tried to create an indexer to access a 'SortedList' of 'List' of 'own class' like this:

var vect = new vectAttivita();
//insert like this
vect["John"] = {"math","3cat",30,""};
//read like this
string _class = activity["John"][0].materia;
//and read like this too
string _class = activity[0][0].materia;

My code is updated since last post at this point:

public class vectAttivita
{
    public SortedList<string, listAttivita> _Inner = new SortedList<string, listAttivita>();

    public void Add(string key)
    {
        _Inner.Add(key, null);
    }

    public SortedList<string, listAttivita> this[int i]
    {
        get
        {
            return _Inner.Values[i]; // <-- Error 1
        }
        set
        {
            _Inner.Values[i] = value; // <-- Error 2
        }
    }

}

public class listAttivita
{
    //public string nome;
    public List<Attivita> listAtt = new List<Attivita>();
    public listAttivita()
    {
        //this.nome = nomeAttivit;
    }
    public void Add(string materia, string ufc, ushort ore, string aulaLabo)
    {
        listAtt.Add(new Attivita(materia, ufc, ore, aulaLabo));
    }
    public void Add(Attivita att)
    {
        listAtt.Add(att);
    }

}

public class Attivita
{
    public string materia;
    public string ufc;
    public ushort ore;
    public string aulaLab;
    public Attivita(string materia, string ufc, ushort ore, string aulaLabo)
    {
        this.materia = materia;
        this.ufc = ufc;
        this.ore = ore;
        this.aulaLab = aulaLabo;
    }

}

In english is :"cs0029 cannot implicitly convert type ..."

Error1: Errore CS0029 Non è possibile convertire in modo implicito il tipo 'Estrazione_Excel01.listAttivita' in 'System.Collections.Generic.SortedList'

Error2: Errore CS0029 Non è possibile convertire in modo implicito il tipo 'System.Collections.Generic.SortedList' in 'Estrazione_Excel01.listAttivita'

How con i get out from this ?


Solution

  • Checkout Indexers and how to implement them.