StreamWriter和StreamReader以及阵列问题


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

问题是它没有写入文本文件,当我打开FrmDc时,FrmMarvel也会打开。当我关闭FrmDc时,显示了Index was outside the bounds of the arrays消息。可能出了什么问题?阅读或多或少也是有效的,因为当我在文本框中写入一些信息,并且源文件包含详细信息时,它会复制现有的文本。

我想我可以删除代码的某些部分,但我不确定是哪一部分。

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

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

CsDc有一个额外的数组。

我认为您得到重复是因为cbKiado_SelectedIndexChanged()中的if...

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

如果只想在文本为DC时运行{...}代码块,则需要删除;。当您在真实情况下运行空块并始终运行{...}时,您现在就有了这种情况。我想这会使文件读两遍。

最新更新