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

时间:2016-02-21 22:45:15

标签: c# list

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

输出是:

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

Box class:

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

2 个答案:

答案 0 :(得分:3)

在每个collection对象中创建一个Box,并在Box的构造函数中添加一个注释。

该集合应该是Box类中的全局或静态的。静态意味着所有框对象共享同一个集合对象。 (如果这真的是你想要的那样)

class Box
{
    private static List<Note> collection = new List<Note>();
    //      ^^^^^^
    ...
}

但是,如果您希望能够在同一个框中添加多个注释,则必须能够将多个注释传递给构造函数,或者使用其他方法允许您在构造框之后添加注释。

注意:每次创建 new 对象时,构造函数都会执行一次。因此,您无法在同一对象上重复调用构造函数。请参阅:Constructors (C# Programming Guide)

可能你应该像这样重构你的代码

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

将框作为参数

传递
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);
}

这意味着您在输入循环之前使用new创建一次框!

将逻辑与输入和输出以及数据保持分开也会更好。现在你的盒子不仅是笔记的数据结构,而且在添加笔记时它还在控制台上做了一些输出。是否真的是写入控制台的盒子任务?您可以让方框通知您事件。

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

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语法。

答案 1 :(得分:1)

您的代码存在的问题是每次使用“new Box(addnote);”你正在创建一个“Box”类的新实例,因此是一个新的空“集合”列表。要更正此问题,请在“Box”类中创建一个方法以添加到“集合”。

public void AddNote(Note addnote)
{
    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 box = new Box(addnote);//Creates the instance of the "Box" class.

//其他内容。

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