简体   繁体   中英

How to Create an Instance of a Class from a Text File?

This is an Appointments class that inherits from IAppointments. I'm trying to write to a text file called Appointments.txt so that I can store appointments within a text file. The issues I have are: The line " _AppList.Add(Line) " kicks out an error "The best overloaded method match for' System.Collections.Generic.ICollection<Calendar.IAppointment>.Add(Calendar.IAppointment) " meaning that I need to pass in an IAppointment rather than the string line, I'm unsure as to how I go about doing this. Secondly, I get an error on my foreach loop saying "Cannot convert type ' Calendar.IAppointment ' to ' string '. All I want to do is read in each string so that I can create a new instance of an appointment and save that instance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;
using System.Drawing;
using System.Collections;
using System.IO;

namespace Calendar
{
    class Appointments : IAppointments
    {
    string line;
    DateTime nStart;
    int nLength;
    string nSubject;
    string nLocation;
    DateTime nDate;

    public Appointments()
    {

    }               

    IList<IAppointment> listApps = new List<IAppointment>();
    IList<IAppointment> _AppList = new List<IAppointment>();

    public bool Load()
    {
        using (var fileReader = new StreamReader(@"D:\All Documents\CalendarApplication_StartingSolution\Appointments.txt"))
        {
            line = fileReader.ReadLine();
            if (line != null)
            {
                string s = line;
                string[] split = s.Split('\t');
                nDate = Convert.ToDateTime(split[0]);
                nStart = Convert.ToDateTime(split[1]);
                nLength = Convert.ToInt32(split[2]);
                nSubject = split[3];
                nLocation = split[4];
                listApps.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate));
                return true;
            }
        }            
        return false;
    }

    public bool Save()
    {
        string sStart;
        string sLength;
        string sDisplayableDescription;

        StreamWriter fileWriter = new StreamWriter(@"D:\All Downloads\CalendarApplication_StartingSolution\Appointments.txt");

        foreach (IAppointment a in listApps)
        {
            sStart = a.Start.ToString();
            sLength = a.Length.ToString();
            sDisplayableDescription = a.DisplayableDescription.ToString();

            string words = sStart + "\t" + sLength + "\t" + sDisplayableDescription;
            fileWriter.WriteLine(words);
        }

        fileWriter.Close();

        StreamReader fileReader = new StreamReader(@"D:\All Documents\CalendarApplication_StartingSolution\Appointments.txt");
        line = fileReader.ReadLine();

        _AppList.Clear();
        while (line != null)
        {
            _AppList.Add(line);
            line = fileReader.ReadLine();
        }

        fileReader.Close();

        IList<IAppointment> replicaList = new List<IAppointment>();
        replicaList.Clear();

        foreach (string word in _AppList)
        {
            string s = word;
            string[] split = s.Split('\t');
            nDate = Convert.ToDateTime(split[0]);
            nStart = Convert.ToDateTime(split[1]);
            nLength = Convert.ToInt32(split[2]);
            nSubject = split[3];
            nLocation = split[4];
            replicaList.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate));
        }

        bool copy = false;

        for (int i = 0; i < listApps.Count; i++)
        {
            if (listApps[i].Start != replicaList[i].Start && listApps[i].Length != replicaList[i].Length && listApps[i].DisplayableDescription != replicaList[i].DisplayableDescription);
            {
                copy = false;
                break;
            }

            copy = true;
        }

        return copy;        
    }

    public void Clear()
    {
        listApps.Clear();
    }

    IEnumerable<IAppointment> IAppointments.GetAppointmentsOnDate(DateTime date)
    {
        //_AppList.Clear(); wipes the _AppList before it is repopulated to avoid recurring appointments.
        _AppList.Clear();
        //Using a var within IEnumerable, allows the compiler to determine the type of variable.
        //Eg "var i = 10;", the compiler determines that i is an integer.
        foreach (var appointment in listApps)
        {
            if (appointment.Start.Date == date.Date)
            {
                _AppList.Add(appointment);
            }
        }
        return _AppList;
    }

    public void Add(IAppointment item)
    {
        listApps.Add(item);
    }

    //Indexes the item selected in listApps
    public int IndexOf(IAppointment item)
    {
        return listApps.IndexOf(item);
    }

    public void RemoveAt(int index)
    {
        listApps.RemoveAt(index);
    }

    public void Insert(int index, IAppointment item)
    {
        listApps.Insert(index, item);
    }

    public void CopyTo(IAppointment[] array, int arrayIndex)
    {
        listApps.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get
        {
            return listApps.Count;
        }
    }

    public bool Contains(IAppointment item)
    {
        return listApps.Contains(item);
    }

    public bool IsReadOnly
    {
        get
        {
            return listApps.IsReadOnly;
        }
    }

    public bool Remove(IAppointment item)
    {
        return listApps.Remove(item);
    }

    public IEnumerator<IAppointment> GetEnumerator()
    {
        return listApps.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        yield return GetEnumerator();
    }

    public IAppointment this[int index]
    {
        get
        {
            return listApps[index];
        }
        set
        {
            listApps[index] = value;
        }
    }
}

Generally you have already your solution in your code:

  1. First Problem:

    while (line != null) { _AppList.Add(line); line = fileReader.ReadLine(); }

This wont work because line is a string and this method wont accept it. Therefor it should rather be:

while (line != null)
            {
                string s = line;
                string[] split = s.Split('\t');
                nDate = Convert.ToDateTime(split[0]);
                nStart = Convert.ToDateTime(split[1]);
                nLength = Convert.ToInt32(split[2]);
                nSubject = split[3];
                nLocation = split[4];
                listApps.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate));
                line = fileReader.ReadLine();
            }

(but I'm not sure for this as I dont know how the file you want to read looks like)

  1. Second Problem:

    foreach (string word in _AppList) { string s = word; string[] split = s.Split('\\t'); nDate = Convert.ToDateTime(split[0]); nStart = Convert.ToDateTime(split[1]); nLength = Convert.ToInt32(split[2]); nSubject = split[3]; nLocation = split[4]; replicaList.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate)); }

could be simplfied into:

foreach(IAppointment date in _AppList)
{
    replicaList.Add(date);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM