3

私は、猫または犬のいずれかを作成し、それを作成してリストに追加する必要がある動物保護アプリケーションを作成しています。リストのすべての動物を表示するボタンと、すべての犬を表示する別のボタンがあります。この 2 つのボタンのいずれかをクリックすると、最後に作成された動物だけが表示されます。ご案内いただけますか?

    namespace AnimalShelter
    {
    class AnimalShelter
    {
    private string Name;
    private int TelNumber;
    private List<Animal> AnimalList;

    public AnimalShelter(string name, int telnumber)
    {
        this.AnimalList = new List<Animal>();
        this.Name = name;
        this.TelNumber = telnumber;
    }

    public Animal FindAnimal(string id)
    {
        foreach (Animal anim in this.AnimalList)
        {
            if (id == anim.ChipRegistrationNumber)
            {
                return anim;
            }
        }
        return null;
    }



    public bool RemoveAnimal(string id)
    {
        if (id.Length <= 0)
            return false;
        Animal ao = this.FindAnimal(id);
        if (ao == null)
            return false;
        this.AnimalList.Remove(ao);
        return true;
    }

    public bool AddNewAnimal(Animal ao)
    {
        if (ao == null)
            return false;

        if (this.FindAnimal(ao.ChipRegistrationNumber) != null)
            return false;

        this.AnimalList.Add(ao);

        return true;
    }




    public List<Dog> GetAllDogs()
    {
       List<Dog> temp = new List<Dog>();
       foreach (Animal animal in this.AnimalList)
       {
           if (animal.GetType() == typeof(Dog))
               temp.Add((Dog)animal);

       }
       return temp;
    }

    public List<Cat> GetAllCats()
    {
        List<Cat> temp = new List<Cat>();
        foreach (Animal animal in this.AnimalList)
        {
            if (animal.GetType() == typeof(Cat))
                temp.Add((Cat)animal);
        }
        return temp;
    }

    public List<Animal> GetAllAnimals()
    {
        return AnimalList;
    }

    }
}

これが実際のフォームで、猫または犬を作成し、すべての動物を表示するか、すべての犬を表示するボタンがあります。

   private void AnimalCreation_Click_1(object sender, EventArgs e)
    {
        string name = tbname.Text;
        string number = tbregno.Text;
        DateTime date = this.DateBroughtIn.Value.Date;
        DateTime lastwalked = this.LastWalked.Value.Date;
        string badhabits = tbbadhabbits.Text;

        if (name.Length <= 0)
        {
            MessageBox.Show("A Name Must Be Given");
        }

        if (number.Length <= 0)
        {
            MessageBox.Show(" A number must be given ");
        }

        if (date > DateTime.Now)
        {
            MessageBox.Show("Invalid date, can not be added");
        }
        else
        {
            Animal a = null;
            if (dog.Checked)
            {

                a = new Dog(number, date, name, lastwalked);
                this.MyAnimalShelter.AddNewAnimal(a);
                MessageBox.Show("Dog Successfully added");
            }
            if (cat.Checked)
            {
                if (badhabits.Length <= 0)
                {
                    MessageBox.Show("Bad Habbits must be filled");
                }
                else
                {
                    a = new Cat(number, date, name, badhabits);
                    this.MyAnimalShelter.AddNewAnimal(a);
                    MessageBox.Show("Cat Successfully added");
                }
            }

        }
    }

    private void AllAnimalsShow_Click(object sender, EventArgs e)
    {
        foreach (Animal animal in MyAnimalShelter.GetAllAnimals())
        {
            listBox1.Items.Clear();
            listBox1.Items.Add(animal);
        }
    }

    private void AllDogsShow_Click_1(object sender, EventArgs e)
    {
        foreach (Animal animal in MyAnimalShelter.GetAllDogs())
        {
            listBox1.Items.Clear();
            listBox1.Items.Add(animal);
        }
    }
4

4 に答える 4

4

foreachデータ構造内のすべての動物について、リストをクリアして1匹の動物を追加するため、繰り返しごとにリストをクリアします。そのため、リストの最後の動物だけが表示されます。

次のようにする必要があります。

listBox1.Items.Clear();
foreach (Animal animal in MyAnimalShelter.GetAllAnimals())
{
    listBox1.Items.Add(animal);
}
于 2013-02-28T09:23:31.243 に答える
0

これは、動物ごとに foreach ループでリストをクリアするためです! ループに入る前にクリアする必要があります!

listBox1.Items.Clear();
foreach (Animal animal in MyAnimalShelter.GetAllAnimals())
{
    listBox1.Items.Add(animal);
}
于 2013-02-28T10:23:42.527 に答える
0

とのforeachループ内でリストをクリアします。AllAnimalsShow_Click()AllDogsShow_Click_1()

ループのにクリアする必要があります。

于 2013-02-28T09:25:03.503 に答える
0

これは、ブロックAllDogsShow_click_1()で呼び出すためです。listBox1.Items.Clear() foreach

それを の前に移動して、foreach何が起こるか見てみましょう。と同じAllAnimalsShow_Click()です。

于 2013-02-28T09:27:00.063 に答える