1

I'm currently stuck with a small problem.

My program currently outputs a return value and puts it in a listbox with this method:

        public override string ToString()
    {
        CheckKamer();
        if (IsKamerBezet == true)
        {
            return $"Kamer: {Nummer} is bezet";
        }
        else
        {
            return $"Kamer: {Nummer} is vrij";
        }
    }

after that my program saves the return values provided by the method in a .txt file with this method:

private void ReserveringText()
    {
        using (System.IO.StreamWriter file =
        new System.IO.StreamWriter(filepath, true))
            file.WriteLine(reserveringLB.Items.ToString());
        System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(filepath);
        foreach (var item in reserveringLB.Items)
        {
            SaveFile.WriteLine(item.ToString());
        }
    }

Now when I restart my program it reads the lines put in the .txt file and sets it back in the listbox, with another simple method:

        private void LaadItems()
    {
        var lines = File.ReadAllLines(filepath);
        reserveringLB.DataSource = lines;
    }

But whenever I try to add a new guest to my hotel, it says the object reference isnt set to an instance of the object, because it loads it from the .txt file and not from the class list I used to load it from.

My guest is added to the class here:

private void reserveerBtn_Click(object sender, EventArgs e)
{
    Gast gast = new Gast(tbNaam1.Text, cbOntbijt1.Checked, cbDiner1.Checked, (Model.Enum.Geslacht)cbGeslacht1.SelectedValue);
    Reservering reservering = new Reservering(vrijekamer, dateTimePicker1.Value, dateTimePicker2.Value, gast);
                    gast.Reservering = reservering;
    admin.VoegGastToe(gast);
     admin.VoegReserveringToe(reservering);
}

Could anyone help me out and tell me how I'd be able to let the program load the .txt file, and put that back into the List so I can actually add a guest to the program.

If anything is unclear please let me know, I'll be looking at this post alot so I can immediatly change it / add something if u guys need anything.

  • `whenever I try to add a new guest to my hotel, it says the object reference isnt set to an instance of the object, because it loads it from the .txt file and not from the class list I used to load it from.` - Where does this happen? – Jimenemex Jan 02 '19 at 19:37
  • It sounds like you are trying to work out a form of serialization but reading a text file into a List just results in a List of Strings. You need to (re)create the class objects...or just serialize the list – Ňɏssa Pøngjǣrdenlarp Jan 02 '19 at 19:37
  • @Jimenemex just addded that part, thank you – Job van Roozendaal Jan 02 '19 at 19:39
  • @WelcomeOverflow Yea it's whenever I start it, when there's no rooms yet, it makes 314 rooms, with "isOccupied" true / false, and puts that in 314 lines to the listbox, then it saves the listbox to the .txt file. But I'm just wondering how to re-create the list with the values from the listbox. – Job van Roozendaal Jan 02 '19 at 19:42
  • 2
    He has already explained how. You need to either serialize the class objects to and from the text file or create a method for taking the string value and converting it back to the class. – hawkstrider Jan 02 '19 at 19:48
  • @bhmahler yea and that's where I'm stuck at, how would I do that? – Job van Roozendaal Jan 02 '19 at 19:52
  • https://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object-to-from-file – hawkstrider Jan 02 '19 at 19:53
  • I would fire up the magic goggle answer machine and use the magic words *serialize c#* for starters. – Ňɏssa Pøngjǣrdenlarp Jan 02 '19 at 20:05

0 Answers0