我想讓我的程序變得更短,那麼它就是現在。 我做了8個不同的對象,並與他們做事情,但我想使它像一個for循環和索引號。 有沒有辦法做到這一點,因爲我查了它,但似乎沒有發現任何東西。如何使用索引編號創建對象
對象和這樣的對象在荷蘭很抱歉。
private void AankomstButton_Click(object sender, EventArgs e)
{
if (welkKind == 1)
{
if (File.Exists(@"Bestanden/" + kind1.Naam + "_" + DateTime.Now.ToString("Y") + ".txt") == false)
{
using (StreamWriter sw = new StreamWriter(@"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt")) ;
}
else
{
using (StreamWriter sw = File.AppendText(@"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
if (welkKind == 2)
{
if (File.Exists(@"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
using (StreamWriter sw = File.AppendText(@"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
else
{
using (StreamWriter sw = new StreamWriter(@"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
if (welkKind == 3)
{
if (File.Exists(@"Bestanden/" + kind3.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
using (StreamWriter sw = File.AppendText(@"Bestanden/" + kind3.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
else
{
using (StreamWriter sw = new StreamWriter(@"Bestanden/" + kind3.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
if (welkKind == 4)
{
if (File.Exists(@"Bestanden/" + kind4.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
using (StreamWriter sw = File.AppendText(@"Bestanden/" + kind4.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
else
{
using (StreamWriter sw = new StreamWriter(@"Bestanden/" + kind4.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
if (welkKind == 5)
{
if (File.Exists(@"Bestanden/" + kind5.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
using (StreamWriter sw = File.AppendText(@"Bestanden/" + kind5.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
else
{
using (StreamWriter sw = new StreamWriter(@"Bestanden/" + kind5.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
什麼是kind2.naam?它是一類資源還是屬性? –