简体   繁体   中英

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. Some reason while I'm closing FrmDc Index was outside the bounds of the arrays message is shown. 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:

    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.

I think you got duplicates because of if... in cbKiado_SelectedIndexChanged()

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

You need to remove ; if you would like to run {...} code block only if text is 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.

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