Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将元素添加到不同位置的列表中_C#_List - Fatal编程技术网

C# 将元素添加到不同位置的列表中

C# 将元素添加到不同位置的列表中,c#,list,C#,List,我想在我的类中调用一个构造函数来将对象添加到列表中,但是目前,列表一直在覆盖自身而不是扩展 输出为: "Note is: " + element.Note + " = priority" + element.Priority 我想实现: "Note is: " + element.Note + " = priority" + element.Priority //of FIRST note 如果我再次运行代码,我希望保存第一个注释,同时显示第二个注释: "Note is: " + eleme

我想在我的类中调用一个构造函数来将对象添加到列表中,但是目前,列表一直在覆盖自身而不是扩展

输出为:

"Note is: " + element.Note + " = priority" + element.Priority
我想实现:

"Note is: " + element.Note + " = priority" + element.Priority //of FIRST note
如果我再次运行代码,我希望保存第一个注释,同时显示第二个注释:

"Note is: " + element.Note + " = priority" + element.Priority
"Note is: " + element.Note + " = priority" + element.Priority
主要方法:

private static void utskrift(int choice)
        {
            if (choice== 1)
            {
                Console.WriteLine("\nPut in the note: ");
                string notering = Console.ReadLine();
                Console.WriteLine("\nEnter the priority: ");
                int priority= Convert.ToInt32(Console.ReadLine());
                Note addnote = new Note(note, priority);
                Box putinbox = new Box(addnote);
                Console.WriteLine("Would you like to submit a new note?");
                int choice= Convert.ToInt32(Console.ReadLine());
                utskrift(choice);
注:类别:

public Note(string note, int priority)
{
    this.note = notering;
    this.priority = prio;                
}
箱类:

class Box
{
   List<Note> collection = new List<Note>();

    public Box(Note addnote)
    {
        Collection.Add(addnote);
        Console.WriteLine("Note has been added!\n");
        foreach (var element in collection)
        {
            Console.WriteLine("Note is: " + element.Note + " = priority" + element.Priority);
        }
类框
{
列表集合=新列表();
公用信箱(注-加注)
{
集合。添加(addnote);
Console.WriteLine(“已添加注释!\n”);
foreach(集合中的var元素)
{
Console.WriteLine(“注释为:“+element.Note+”=priority”+element.priority);
}

代码的问题是,每次使用“new Box(addnote);”时,您都在创建“Box”类的新实例,从而创建一个新的空“collection”列表。为了纠正这一问题,请在“Box”类中创建一个方法以添加到“collection”

然后,在添加新注释时,只需将“Box”类的前一个实例与新方法一起使用即可。例如:

Box box = new Box(addnote);//Creates the instance of the "Box" class.
//中间还有其他东西

box.AddNote(addnote);//Add another note to the previous instance of "Box".

在每个
对象内创建一个
集合
,并在
的构造函数中向其添加一个注释

Box
类中,集合应该是全局的或静态的。静态意味着所有Box对象共享同一个集合对象。(如果这确实是您想要的)

然后像这样称呼它

class Box
{
    List<Note> collection = new List<Note>();

    public void AddNote(Note note)
    {
        collection.Add(note);
        Console.WriteLine("Note has been added!\n");
        foreach (var element in collection)
        {
            Console.WriteLine("Note is: " + element.Note + " = priority" + element.Priority);
        }
    }
}            
Box box = new Box();
while(true)
{
    int choice = <read the user choice>; // Pseudo code
    if (<the user wants to terminate>)
        break;
    utskrift(box, choice);
}
NotifyingBox
类可以这样使用:

public static void Test()
{
    NotifyingBox box = new NotifyingBox();
    box.NoteAdded += Box_NoteAdded;
    box.AddNote(new Note { Text = "aaa", Priority = 1 });
    box.AddNote(new Note { Text = "bbb", Priority = 2 });
}

private static void Box_NoteAdded(NotifyingBox box)
{
    Console.WriteLine();
    Console.WriteLine("Note has been added!");
    foreach (Note note in box.Notes) {
        Console.WriteLine($"  Note is: {note.Text} = priority{note.Priority}");
    }
}
现在,当一张便条被添加到盒子中时,你可以做不同的事情,而不必改变盒子。盒子保持干净,只做盒子里的事情



注意:我正在使用Visual Studio 2015中提供的新C#6.0语法。

您需要清理代码,并显示是否从入口点开始以及最初调用的内容。我还建议使用调试器逐步检查代码。查明问题和/或错误,并告诉我们哪一行问题正在发生。如果要显示类,请显示所有类,而不仅仅是Box类。
集合。添加(注意);
这是打字错误吗?请提供。目前没有示例显示将
注释添加到现有的
Box
(这似乎是您实际遇到问题的部分)。谢谢你的回答,我已经更新了我的主要方法。如果可以的话,请仔细查看。你为什么不发布代码的所有部分..你忽略了所有的结束
}
请使用调试器逐步检查您的代码。您是否尝试先执行此操作?谢谢您的回答!我的输出现在可以正常工作。稍后,我希望在程序执行时保存列表中的所有内容,并且在下次启动时,我希望能够编辑notes优先级,我当前的设计是否适用于此我会尝试不同的方法吗?
private static void utskrift(Box box, int choice)
{
    if (choice== 1)
    {
        ...
        Note addnote = new Note(note, priority);
        box.AddNote(addnote);
        ...
    ...
}
Box box = new Box();
while(true)
{
    int choice = <read the user choice>; // Pseudo code
    if (<the user wants to terminate>)
        break;
    utskrift(box, choice);
}
public class NotifyingBox
{
    private List<Note> notes = new List<Note>();

    public event Action<NotifyingBox> NoteAdded;

    public IEnumerable<Note> Notes => notes;

    public void AddNote(Note note)
    {
        notes.Add(note);
        NoteAdded?.Invoke(this);
    }
}
public static void Test()
{
    NotifyingBox box = new NotifyingBox();
    box.NoteAdded += Box_NoteAdded;
    box.AddNote(new Note { Text = "aaa", Priority = 1 });
    box.AddNote(new Note { Text = "bbb", Priority = 2 });
}

private static void Box_NoteAdded(NotifyingBox box)
{
    Console.WriteLine();
    Console.WriteLine("Note has been added!");
    foreach (Note note in box.Notes) {
        Console.WriteLine($"  Note is: {note.Text} = priority{note.Priority}");
    }
}