简体   繁体   English

“自己的类”的“列表”的“ SortedList”的索引器

[英]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: 我试图创建一个索引器来访问“拥有类”的“列表”的“ SortedList”,如下所示:

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 ..." 英文为:“ cs0029无法隐式转换类型...”

Error1: Errore CS0029 Non è possibile convertire in modo implicito il tipo 'Estrazione_Excel01.listAttivita' in 'System.Collections.Generic.SortedList' 错误1:Errore CS0029在“ System.Collections.Generic.SortedList”中隐式转换为“ Estrazione_Excel01.listAttivita”时无法转换

Error2: Errore CS0029 Non è possibile convertire in modo implicito il tipo 'System.Collections.Generic.SortedList' in 'Estrazione_Excel01.listAttivita' 错误2:Errore CS0029在'Estrazione_Excel01.listAttivita'的隐式或间接模式'System.Collections.Generic.SortedList'中可能的转换

How con i get out from this ? 我怎么能摆脱这个?

结帐索引器以及如何实现它们。

I would just get rid of the SortedList class and keep just the one add methond in your List class that addes a Attivita. 我只是摆脱SortedList类,而只在您的List类中添加一个添加Attivita的方法。 Then for simplicity, create the Attivita before adding it to a Dictionary where the key would be a person's name. 然后为简单起见,先创建Attivita,然后再将其添加到Dictionary,其中的键将是一个人的名字。

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. 接下来,将其添加到词典中的学生,但是首先您需要检查该学生是否存在,否则add方法会产生错误。 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. 这部分很可能在按钮的on_click方法内,并且先前的信息将从用户填写的Windows文本框中填充。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM