0

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 ?

  • Where do you get the "matrAttivita" from? I don't see it in the link which appears to be a nested dictionary inside a dictionary value. Nor is it the class Attivita that is user created. Also the title and word 2-dim is wrong, you are trying to deal with an Array of arrays, or another term is Jagged arrays. [Differences in arrays](http://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays) – Edward Jan 24 '17 at 21:12
  • You are right, i edited, thanks! matrAttivita is writed by me ispired by the code in the link. But i think i found the override i need. – Andrea Canton Jan 24 '17 at 22:04
  • I would ask what you are trying to accomplish by creating an array of an array; because sometimes people go about something the wrong way. If you just want a "List" of differing things (strings/ints). I personally would create a class of say Employees. Then use Dictionary, not only does this give you an ease of index the key but has a value that can contain everything you might want. – Edward Jan 25 '17 at 00:21
  • Maybe was not clear to me to what i wanted to do ! In true is the first time that i try to create a complex data structure like this. I've edited my first post and the question with my progress ... hoping now is more focused ! Thanks a lot ... – Andrea Canton Feb 03 '17 at 17:41
  • First the code `string class =` is not something you want to do as class is a reserved word. Second if you don't know what is being returned just stick to making your statement a type var, so `var temp =`. – Edward Feb 03 '17 at 17:51

2 Answers2

1

Checkout Indexers and how to implement them.

Andrew Diamond
  • 6,295
  • 1
  • 15
  • 33
0

I would just get rid of the SortedList class and keep just the one add methond in your List class that addes a Attivita. Then for simplicity, create the Attivita before adding it to a Dictionary where the key would be a person's name.

Dictionary<string,listAttivita> students = new Dictionary<string,listAttivita>(){};
Attivita newActivity = new Attivita("math","3cat",30,"");
string studentName ="John";

next add that to a student in your Dictionary, but first you need to check the student exists or the add method would create an error. This part would most likely be inside a on_click method for a button and the previous information would be populated from a windows text boxes that are filled in by a user.

if(students.ContainsKey(studentName) )
    students[studentName].Add(newActivity);
else
{
    students[studentName] = new listAttivita(){};
    students[studentName].Add(newActivity);
 }

so now if you wanted to return all the activities a specific student has in a sorted order then.

var studentActivity = students.Where(x => x.Key==studentName).First().Value.listAtt.OrderBy(x => x.materia);
foreach(var parts in studentActivity)
    Console.WriteLine("Materia:{0} UFC:{1} ORE:{2} aulaLab:{3}", parts.materia, parts.ufc, parts.ore, parts.aulaLab);
Edward
  • 864
  • 1
  • 7
  • 29
  • @Andrea Canton Its good decorum to follow with with selecting an answer that helps you with your question. – Edward Mar 28 '17 at 18:22