流阅读器到列表,列表对行值进行数组和拆分,将拆分后的值作为对象并在RichTextBox中将其打印为随机对象

问题描述 投票:0回答:1

流阅读器到列表,列表对行值进行数组和分割,分割后的值作为对象,并在RichTextBox中将其打印为随机对象

c# arrays forms list streamreader
1个回答
0
投票

尝试以下更简单的方法:

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

namespace Övning_3___Tipsmaskinen2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            FileLoader();
        }
        public class Bok
        {
            public string Titel;//Tittle
            public string Författare;//Author
            public string Boktyp;//TBookTYp
            public string ILager;// Bool or InSToridge 

            public Bok(string titel, string författare, string boktyp, string ilager) // method must have a return typ , funkar enbart om jag skriver in void
            {
                this.Titel = titel;
                this.Författare = författare;
                this.Boktyp = boktyp;
                this.ILager = ilager;
            }

            public override string ToString()
            {
                return "\t Titel : " + Titel + "\t Skribent : " + Författare + "  \t Boktyp: " + Boktyp + " \t Finns i lager : " + ILager;
            }
        }
        public List<Bok> FileLoader()
        {
            List<Bok> bokList = new List<Bok>();
            if (File.Exists("texter.txt"))
            {
                StreamReader reader = new StreamReader("texter.txt", Encoding.Default, true);


                string item = "";
                while ((item = reader.ReadLine()) != null)
                {
                    string[] vektor = item.Split(new string[] { "###" }, StringSplitOptions.None);

                    Bok k = new Bok(vektor[0], vektor[1], vektor[2], vektor[3]);
                    bokList.Add(k);
                }
                return bokList;
            }
            return null;

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.