如何从文本文件创建类的实例?

时间:2015-04-11 19:37:13

标签: c# streamreader streamwriter

这是一个继承自IAppointments的Appointments类。 我试图写一个名为Appointments.txt的文本文件,这样我就可以在文本文件中存储约会。我遇到的问题是: 该行&#34; _AppList.Add(Line)&#34;发出错误&#34;最佳重载方法匹配&#39; System.Collections.Generic.ICollection<Calendar.IAppointment>.Add(Calendar.IAppointment)&#34;这意味着我需要传递一个IAppointment而不是字符串,我不确定我是如何做到这一点的。 其次,我在foreach循环中收到错误说&#34;无法转换类型&#39; Calendar.IAppointment&#39;到&#39; string&#39;。我想要做的只是在每个字符串中读取,以便我可以创建一个新的约会实例并保存该实例。

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

1 个答案:

答案 0 :(得分:0)

通常,您的代码中已经有了解决方案:

  1. 第一个问题:

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

  2. 这不会起作用,因为line是一个字符串,这个方法不会接受它。 因此,它应该是:

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

    (但我不确定这个,因为我不知道你想要阅读的文件是什么样的)

    1. 第二个问题:

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

    2. 可以简化为:

      foreach(IAppointment date in _AppList)
      {
          replicaList.Add(date);
      }
      
相关问题