2013-02-28 86 views
3

我正在製作一個動物收容所應用程序,其中我必須創建一個貓或一隻狗,並創建它並將其添加到列表中。我有一個按鈕,應該顯示列表中的所有動物,另一個按鈕應該顯示所有的狗。當我點擊這兩個按鈕時,只有最後創建的動物出現。你能給我一些指導嗎?C#簡單列表顯示的問題

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); 
     } 
    } 
+1

嚴重,而不是通過列表循環試圖找到實體,應使用所提供的方法使用lambda表達式,比如:'this.AnimalList.Where(K => k.ChipRegistrationNumber == ID).FirstOrDefault ();' – Knelis 2013-02-28 09:25:12

+0

我在第一年計算機科學,我們還沒有得到這些東西。我正在使用最基本的東西,直到現在我學到了:) – 2013-02-28 09:26:29

+1

使用'String.IsNullOrEmpty(s)'而不是's.Length <= 0'來檢查一個字符串。這至少消除了NullReferenceExceptions的風險。 – PVitt 2013-02-28 09:44:39

回答

4

清除列表中的foreach的每一次迭代,從而爲每一個在數據結構清除列表,並添加一個獸獸。這就是爲什麼最終只有列表中的最後一隻動物。

你需要做的是這樣的:

listBox1.Items.Clear(); 
foreach (Animal animal in MyAnimalShelter.GetAllAnimals()) 
{ 
    listBox1.Items.Add(animal); 
} 
0

您清除AllAnimalsShow_Click()AllDogsShow_Click_1()foreach循環內的列表。

應在之前清除循環。

0

這是因爲在你AllDogsShow_click_1()你叫listBox1.Items.Clear()foreach塊。

將其移至foreach之前,看看會發生什麼。與AllAnimalsShow_Click()相同。

0

這是因爲您在每個動物的foreach循環中清除您的列表! 你必須在進入循環之前清除它!

listBox1.Items.Clear(); 
foreach (Animal animal in MyAnimalShelter.GetAllAnimals()) 
{ 
    listBox1.Items.Add(animal); 
}