将所有类对象数组值添加到文本文件中

时间:2018-03-16 03:24:12

标签: c#

我想将在课程类实例化期间分配的所有属性值添加到文本文件中。

 CourseManager courseMng = new CourseManager();


        Course prog1 = new Course();
        Console.WriteLine(prog1.GetInfo());
        Console.WriteLine();


        prog1.CourseCode = "COMP100";
        prog1.Name = "Programming 1";
        prog1.Description = "Programming1 description";
        prog1.NoOfEvaluations = 3;
        Console.WriteLine(prog1.GetInfo());


        Course prog2 = new Course("COMP123", "Programming2") { Description = "prog 2 desc", NoOfEvaluations = 2 };
        Console.WriteLine(prog2.GetInfo());



        courseMng.AddCourse(prog1);
        courseMng.AddCourse(prog2);

这是我的主要内容,这是我的CourseManager课程

Course[] courses;
    int numberOfCourses;


    public int NumberOfCourses
    {
        get { return numberOfCourses; }
        set { numberOfCourses = value; }

    }

    public Course[] Courses
    {
        get
        {
            return courses;
        }

        set
        {
            courses = value;
        }
    }

    public CourseManager()
    {
        Courses = new Course[100];

    }


     public void AddCourse(Course aCourse)
    {

        Courses[numberOfCourses] = aCourse;
        numberOfCourses++;
        aCourse.Manager = this;

    }

    public void ExportCourses(string fileName, char Delim)
    {


        FileStream stream = null;
        StreamWriter writer;
        try
        {
            stream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            writer = new StreamWriter(stream);
            Course aCourse = new Course();
                for(int i =0; i<numberOfCourses;i++)
            {

               //writer.WriteLine(courses.ToString());
                writer.WriteLine("{0}{1}{2}{1}{3}{1}{4}", aCourse.CourseCode, Delim, aCourse.Name, aCourse.Description, aCourse.NoOfEvaluations);
            }


            writer.Close();
        }

        finally
        {
            stream.Close();
        }


    }

所以问题是当我尝试写信时它只是打印空值。我希望将CourseCode,Name,Descrpition和NumberOfEvaluations写入.txt文件中。 如果您需要任何其他代码,请告诉我

提前致谢

2 个答案:

答案 0 :(得分:0)

您的问题在于从

行获取数据
Course aCourse = new Course();
for(int i =0; i<numberOfCourses;i++)
{
    //writer.WriteLine(courses.ToString());
    writer.WriteLine("{0}{1}{2}{1}{3}{1}{4}", aCourse.CourseCode, Delim, aCourse.Name, aCourse.Description, aCourse.NoOfEvaluations);
}

问题是你要写出aCourse属性的值,它们永远不会设置为new Course()以外的任何值(按照第一行)。

尝试将其更改为此

for(int i =0; i<numberOfCourses;i++)
{
    Course aCourse = Courses[i];
    //writer.WriteLine(courses.ToString());
    writer.WriteLine("{0}{1}{2}{1}{3}{1}{4}", aCourse.CourseCode, Delim, aCourse.Name, aCourse.Description, aCourse.NoOfEvaluations);
}

在第二个例子中,我们将aCourse的值设置为迭代器中的下一个过程。

根据您的评论

  

我有一个包含不同课程的文件,我想将其添加到课程中   阵列。我这样做

Course C = new Course(); 
C.CourseCode = reader.ReadLine(); 
C.Name = reader.ReadLine();
C.Description = reader.ReadLine(); 
C.NoOfEvaluations = Int32.Parse(reader.ReadLine()); 
courses[index++] = C; 
     

但是我在NoofEvaluations的任何解决方案中都收到了无效的输入错误?

这里的问题是您通过writer.WriteLine方法将数据导出到单行中,这些方法用分隔符|或更确切地用'char Delim`参数分隔值。在这种情况下,您需要将一行读入由相同字符拆分的数组中。有点像。

var line = reader.ReadLine();
var array = line.Split('|'); // or char Delim if you prefer
var c = new Course();
c.CourseCode = array[0];
c.Name = array[1];
c.Description = array[2];
c.NoOfEvaluations = int.Parse(array[3]);
courses[index++] = c;

答案 1 :(得分:0)

我会建议使用不同的代码来清理问题,而不是解决问题。我会简化如下:

public class Course
{
    public string CourseCode { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int NoOfEvaluations { get; set; }
    public IEnumerable<Course> Courses { get; set; }

    public Course()
    {
        Courses = new List<Course>();
    }

    //this will format the text to your specification
    public string ToDelimitedString(char Delimiter)
    {
        return String.Format("{0}{4}{1}{4}{2}{4}{3}{5}"
            , this.CourseCode
            , this.Name
            , this.Description
            , this.NoOfEvaluations.ToString()
            , Delimiter
            , Environment.NewLine);
    }

    public void Export(string FileName, char Delimiter)
    {
        //this will be more efficient than writing on line at a time
        var coursesExport = new StringBuilder();

        foreach(var course in this.Courses)
        {
            coursesExport.Append(course.ToDelimitedString(Delimiter));
        }

        File.WriteAllText(FileName, coursesExport.ToString());
    }
}