Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding elements to list in different places [closed]

Tags:

c#

list

I want to call a constructor in my class to add objects to a list, however at the moment, the lists keep overwriting itself instead of expanding.

Output is:

"Note is: " + element.Note + " = priority" + element.Priority

I want to achieve:

"Note is: " + element.Note + " = priority" + element.Priority //of FIRST note

If i run the code again I want the first note to be saved and the second one displayed as well:

"Note is: " + element.Note + " = priority" + element.Priority
"Note is: " + element.Note + " = priority" + element.Priority

Main method:

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

Note class:

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);
        }
like image 348
Teddy981 Avatar asked Mar 24 '26 22:03

Teddy981


1 Answers

One collection is created inside each Box object and one note is added to it in the constructor of Box.

The collection should either be global or static within the Box class. Static means that all box objects are sharing the same collection object. (If this is really what you want)

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

If, however, you want to be able to add several notes to the same box, you must either be able to pass several notes to the constructor or have an additional method allowing you to add notes after the box has been constructed.


Note: The constructor is executed once each time you create a new object. So you cannot call the constructor repeatedly on the same object. See: Constructors (C# Programming Guide)


Probably you should refactor your code like this

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

Pass the box as parameter

private static void utskrift(Box box, int choice)
{
    if (choice== 1)
    {
        ...
        Note addnote = new Note(note, priority);
        box.AddNote(addnote);
        ...
    ...
}

And then call it like this

Box box = new Box();
while(true)
{
    int choice = <read the user choice>; // Pseudo code
    if (<the user wants to terminate>)
        break;
    utskrift(box, choice);
}

This means that you create the box once with new before the entry loop!


It would also be better to separate the logic from input and output and from data keeping. Now your box is not only a data structure for notes but it also does some output on the console when notes are added. Is it really the boxes task to write to the console? You could have the box notify you through an event.

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

The NotifyingBox class can be used like 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}");
    }
}

Now you can do different things when a note has been added to the box without having to change the box. The box is kept clean and does only do box-things.


Note: I am using the new C# 6.0 syntax available in Visual Studio 2015.

like image 165
Olivier Jacot-Descombes Avatar answered Mar 26 '26 13:03

Olivier Jacot-Descombes