简体   繁体   English

StreamWriter和StreamReader以及数组问题

[英]StreamWriter and StreamReader and array problems

I have this very long code, which is works more or less.

The problem is it's not writing into text file, and when I open FrmDc FrmMarvel also opens. 问题是它没有写入文本文件,当我打开FrmDc FrmMarvel也打开了。 Some reason while I'm closing FrmDc Index was outside the bounds of the arrays message is shown. 显示了我关闭FrmDc Index was outside the bounds of the arrays消息Index was outside the bounds of the arrays某些原因。 What could be wrong? 有什么事吗 The reading is works more or less also, because when I write some information to a textbox, and the source file contains details, it duplicates the existing texts. 读取也或多或少起作用,因为当我将一些信息写入文本框,并且源文件包含详细信息时,它将复制现有文本。

I think I can remove some part of the code, but I'm not sure which. 我想我可以删除部分代码,但是我不确定是哪一部分。

namespace Kepregenybolt
    {
        public partial class Form1 : Form
        {
            List<CsMarvel> kLista = new List<CsMarvel>();
            StreamWriter sW;

            public Form1()
            {
                InitializeComponent();
                cbKiado.Items.Add("Marvel");
                cbKiado.Items.Add("DC");
                try
                {
                    StreamReader sR = new StreamReader("kepregenyek.txt", Encoding.UTF8);
                    while (!sR.EndOfStream)
                    {
                        string sor = sR.ReadLine();
                        string[] s = sor.Split(';');
                        if (s.Length == 5)
                        {
                            CsMarvel h = new CsMarvel(s[0],
                                s[1],
                                s[2],
                                Convert.ToInt32(s[3]),
                                Convert.ToInt32(s[4]));
                            kLista.Add(h);
                        }
                        else
                        {
                            CsDc h = new CsDc(s[0],
                               s[1],
                               s[2],
                               Convert.ToInt32(s[3]),
                               Convert.ToInt32(s[4]),
                               s[5]);
                            kLista.Add(h);
                        }
                    }
                    sR.Close();
                    foreach (CsMarvel item in kLista)
                    {
                        listBox1.Items.Add(item.listába());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            private void cbKiado_SelectedIndexChanged(object sender, EventArgs e)
            {
                FrmMarvel a = new FrmMarvel(kLista);

                if (cbKiado.Text.Equals("Marvel")) ;



                a.ShowDialog();
                try
                {
                    StreamReader sR = new StreamReader("kepregenyek.txt", Encoding.UTF8);
                    while (!sR.EndOfStream)
                    {
                        string sor = sR.ReadLine();
                        string[] s = sor.Split(';');
                        CsMarvel l = new CsMarvel(s[0], s[1], s[2], Convert.ToInt32(s[3]), Convert.ToInt32(s[4]));
                        kLista.Add(l);
                    }

                    sR.Close();
                    foreach (CsMarvel item in kLista)
                    {
                        listBox1.Items.Add(item.listába());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                } 

                if (cbKiado.Text.Equals("DC"));
                {
                    FrmDc b = new FrmDc(kLista);
                    b.ShowDialog();
                    try
                    {
                        StreamReader sR = new StreamReader("kepregenyek.txt", Encoding.UTF8);
                        while (!sR.EndOfStream)
                        {
                            string sor = sR.ReadLine();
                            string[] s = sor.Split(';');
                            CsDc l = new CsDc(s[0], s[1], s[2], Convert.ToInt32(s[3]), Convert.ToInt32(s[4]), s[5]);
                            kLista.Add(l);
                        }

                        sR.Close();
                        foreach (CsDc item in kLista)
                        {
                            listBox1.Items.Add(item.listába());
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);

                    }
                }
            }


            private void FrmMarvel_FormClosing(object sender, FormClosedEventArgs e)
            {

                sW = new StreamWriter("kepregenyek.txt", false, Encoding.UTF8);
                foreach (CsMarvel item in kLista)
                {
                    sW.WriteLine(item.fájlba());
                }
                sW.Close();
            }

            private void btnClose_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }

The CsMarvel is the following: CsMarvel如下:

    public class CsMarvel
        {
            string cim;
            string iro;
            string rajzolo;
            int megjelenes;
            int ar;




            public CsMarvel(string cim, string iro, string rajzolo, int megjelenes, int ar)
            {
                this.cim = cim;
                this.iro = iro;
                this.rajzolo = rajzolo;
                this.megjelenes = megjelenes;
                this.ar = ar;

            }
            public virtual string fájlba()
            {
                return this.cim + ";" + this.iro + ";" + this.rajzolo + ";" + this.megjelenes + ";" + this.ar;
            }
            public virtual string listába()
            {
                return this.cim + ":" + this.iro + " " + this.rajzolo + " kiadó Kiadva:" + this.megjelenes + " " + this.ar + " Ft";
            }
            public string Cim
            {
                get
                {
                    return cim;

                }
                set
                {
                    cim = value;
                }
            }
            public string Iro
            {
                get
                {
                    return iro;

                }
                set
                {
                    iro = value;
                }
            }
            public string Rajzolo
            {
                get
                {
                    return rajzolo;

                }
                set
                {
                    rajzolo = value;
                }
            }
            public int Megjelenes
            {
                get
                {
                    return megjelenes;

                }
                set
                {
                    megjelenes = value;
                }
            }
            public int Ar
            {
                get
                {
                    return ar;

                }
                set
                {
                    ar = value;
                }
            }
        }

}

And the CsDc has one extra array. CsDc还有一个额外的阵列。

I think you got duplicates because of if... in cbKiado_SelectedIndexChanged() 我认为由于cbKiado_SelectedIndexChanged() if...cbKiado_SelectedIndexChanged()重复

if (cbKiado.Text.Equals("DC"));
{
...
}

You need to remove ; 您需要删除; if you would like to run {...} code block only if text is DC . 仅当文本为DC时才运行{...}代码块。 You have now situation when you run empty block in true case and run {...} always. 您现在遇到的情况是,在真实情况下运行空块并始终运行{...} It will make reading of file twice, I suppose. 我想这将使文件读取两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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