【问题标题】:Indexer for a 'SortedList' of 'List' of 'own class''自己的类'的'List'的'SortedList'的索引器
【发布时间】:2017-06-09 21:23:04
【问题描述】:

我尝试创建一个索引器来访问“自己的类”的“列表”的“排序列表”,如下所示:

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;

我的代码自上次发布以来已更新:

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

}

英文是:“cs0029 不能隐式转换类型...”

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'

我如何摆脱困境?

【问题讨论】:

  • 你从哪里得到“matrAttivita”?我在链接中看不到它,该链接似乎是字典值内的嵌套字典。也不是用户创建的 Attivita 类。标题和单词 2-dim 也是错误的,您正在尝试处理数组数组,或者另一个术语是锯齿状数组。 Differences in arrays
  • 你说得对,我编辑了,谢谢! matrAttivita 是由我在链接中的代码启发下编写的。但我想我找到了我需要的覆盖。
  • 我会问你想通过创建一个数组来完成什么;因为有时人们做事的方式是错误的。如果您只想要不同事物(字符串/整数)的“列表”。我个人会创建一个说员工的类。然后使用 Dictionary,这不仅使您可以轻松索引 键,而且具有可以包含您可能想要的所有内容的值。
  • 也许我不清楚我想做什么!事实上,这是我第一次尝试创建这样的复杂数据结构。我已经编辑了我的第一篇文章和我的进步问题......希望现在更加专注!非常感谢...
  • 首先代码string class = 不是您想要做的事情,因为class 是一个保留字。其次,如果您不知道返回的内容,请坚持将您的语句设为 var 类型,所以 var temp =.

标签: c# dictionary multidimensional-array sortedlist


【解决方案1】:

结帐Indexers 以及如何实现它们。

【讨论】:

  • 谢谢你,安德鲁,你告诉我的方式!
  • 我现在添加索引器......但仍然有一个错误......就像你在上面编辑的问题中看到的那样。
【解决方案2】:

我会去掉 SortedList 类,只在 List 类中保留一个添加 Attivita 的添加方法。然后为简单起见,先创建 Attivita,然后将其添加到字典中,其中键是人名。

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

接下来将它添加到字典中的学生,但首先您需要检查学生是否存在,否则 add 方法会产生错误。这部分很可能在按钮的 on_click 方法中,之前的信息将从用户填写的 windows 文本框中填充。

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

所以现在,如果您想按排序顺序返回特定学生的所有活动。

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

【讨论】:

  • @Andrea Canton 选择一个可以帮助您解决问题的答案是一种很好的礼仪。
猜你喜欢
  • 2011-01-31
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 2012-06-15
相关资源
最近更新 更多