Benjamin Celis
Benjamin Celis

Reputation: 11

how to make objects using an index number

I want to make my program that i made way shorter then it is right now. I make 8 different objects and do things with them but I want to make it with something like a for loop and an index number. Is there a way to do this, because i've looked it up but don't seem to find anything.

The objects and such are in Dutch sorry for that.

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

Upvotes: 1

Views: 33

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156948

If you keep an array of children (kind), you can access the array by index.

Kind[] children = new Kind[] { kind1, kind2 };

if (welkKind >= 0 && welkKind < children.Length)
{
    Kind kind = children[welkKind];

    string fileName = @"Bestanden/" + kind.Naam + "_" + DateTime.Now.ToString("Y") + ".txt";
    if (File.Exists(fileName))
    {
        using (StreamWriter sw = new StreamWriter(fileName))
        {
            sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
        }
    }
    else
    {
        using (StreamWriter sw = File.AppendText(fileName))
        {
            sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
        }
    }
}

Another option would be a list, which would have similar syntax in its usage, but you can dynamically add and remove items from the list:

List<Kind> children = new List<Kind>();
children.Add(kind1);
children.Add(kind2);
children.Add(new Kind() { Naam = "John" });

if (welkKind >= 0 && welkKind < children.Count)
{
    Kind kind = children[welkKind];
}

Upvotes: 1

Related Questions