4 Replies - 477 Views - Last Post: 20 September 2013 - 07:19 PM Rate Topic: -----

#1 breakdown  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-April 13

ListBox won't take in my items correctly

Posted 19 September 2013 - 05:27 PM

I'm trying to make a list of animals which can use a method to get a neat string.Format returned for each item, that way I have more control than just using ListBox.Datasource. But the items don't appear in the list properly. Instead of displaying the correct info, it comes out as '0, , GenderType, CategoryType, 0' in the ListBox, every time. The idea is that the animals - in this example, Cat - inherits from Mammal - which is a category - which inherits from Animal. Each class like Cat should have info like name, ID, etc. But again, it just doesn't turn out right.

The list is in MainForm.cs, seen below. Do note that only Cat is done, the rest will follow when Cat works.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ApuAnimalMotel.AnimalSubTypes;
using ApuAnimalMotel.AnimalTypes;

namespace ApuAnimalMotel
{
	public partial class MainForm : Form
	{	
		CategoryType category;
		int ID;
		List<Animal> animalList; //Can I add inherited classes to this list?
		
		public MainForm()
		{
			InitializeComponent();
			StartUp();
			
			animalList = new List<Animal>();
			category = new CategoryType();
		}
		
		private void StartUp()
		{
			ID = 0;
			
			genderBox.DataSource = Enum.GetValues(typeof(GenderEnum));
			
			categoryBox.DataSource = Enum.GetValues(typeof(CategoryEnum));
			
			subBox.SelectedIndex = 0;
			
			nameBox.Focus();
		}
		
		private int NewID()
		{
			ID++;
			return ID;
		}
		
		private void RefreshList() //Error likely here somewhere, I just can't see it.
		{
			animalsBox.Items.Clear();
			
			for (int i = 0; i < (animalList.Count); i++) 
			{
				animalsBox.Items.Add(animalList[i].GetAnimalSpecificData());
			}
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(nameBox.Text) || string.IsNullOrEmpty(ageBox.Text) || subBox.SelectedIndex == -1)
			{
				MessageBox.Show("Please fill out the entire form.");
			}
			
			else
			{
#region Animals					
				if (categoryBox.SelectedIndex == 0) //Bird
				{
					if (subBox.SelectedIndex == 0) 
					{
						Finch finch = new Finch();
					}
					
					else if (subBox.SelectedIndex == 1)
					{
						Parrot parrot = new Parrot();
					}
				}
				
				else if (categoryBox.SelectedIndex == 1) //Insect
				{
					if (subBox.SelectedIndex == 0) 
					{
						Spider spider = new Spider();
					}
					
					else if (subBox.SelectedIndex == 1)
					{
						WalkingStick walkingStick = new WalkingStick();
					}
				}
				
				else if (categoryBox.SelectedIndex == 2) //Mammal
				{ 
					if (subBox.SelectedIndex == 0) //Cat
					{
						//Cat cat = new Cat(NewID(), nameBox.Text, int.Parse(ageBox.Text), genderBox.Text, categoryBox.Text);
						animalList.Add(new Cat(NewID(), nameBox.Text, int.Parse(ageBox.Text), genderBox.Text, categoryBox.Text)); //Only look at this one! Ignore the rest unless you think there's an error affecting the situation.
					}
					
					else if (subBox.SelectedIndex == 1)
					{
						Dog dog = new Dog();
					}
				}
				
				else if (categoryBox.SelectedIndex == 3) //Marine
				{
					if (subBox.SelectedIndex == 0) 
					{
						Guppy guppy = new Guppy();
					}
					
					else if (subBox.SelectedIndex == 1)
					{
						Tetra tetra = new Tetra();
					}
				}
				
				else if (categoryBox.SelectedIndex == 4) //Reptile
				{
					if (subBox.SelectedIndex == 0) 
					{
						Lizard lizard = new Lizard();
					}
					
					else if (subBox.SelectedIndex == 1)
					{
						Turtle turtle = new Turtle();
					}
				}
#endregion				
				RefreshList();
				
			}


			
		}
		
		void CategoryBoxSelectedIndexChanged(object sender, EventArgs e)
		{
			if (categoryBox.SelectedIndex == 0) //Bird
			{
				subBox.Items.Clear();
				subBox.Items.Add("Finch");
				subBox.Items.Add("Parrot");
			}
			
			else if (categoryBox.SelectedIndex == 1) //Insect
			{
				subBox.Items.Clear();
				subBox.Items.Add("Spider");
				subBox.Items.Add("Walking Stick");
			}
			
			else if (categoryBox.SelectedIndex == 2) //Mammal
			{
				subBox.Items.Clear();
				subBox.Items.Add("Cat");
				subBox.Items.Add("Dog");
			}
			
			else if (categoryBox.SelectedIndex == 3) //Marine
			{
				subBox.Items.Clear();
				subBox.Items.Add("Guppy");
				subBox.Items.Add("Tetra");
			}
			
			else if (categoryBox.SelectedIndex == 4) //Reptile
			{
				subBox.Items.Clear();
				subBox.Items.Add("Lizard");
				subBox.Items.Add("Turtle");
			}			
		}
	}
}



Cat is a class that inherits from Mammal that inherits from Animal that is defined by IAnimal. They look like this:

using System;
using System.Runtime.InteropServices;
using ApuAnimalMotel.AnimalTypes;

namespace ApuAnimalMotel.AnimalSubTypes
{
	/// <summary>
	/// Description of Cat.
	/// </summary>
	public class Cat : Mammal
	{
		int totalClaws, totalWhiskers, clawAmount, legAmount;
		
		public Cat()
		{
			clawAmount = base.ClawAmount;
			legAmount = base.LegAmount;
			totalClaws = (ClawAmount * LegAmount);
			totalWhiskers = 40;
		}
		
		public Cat(int id, string name, int age, string gender, string category) : base()
		{
			id = base.ID;
			name = base.Name;
			age = base.Age;
			
			this.ID = id;
			this.Name = name;
			this.Age = age;
			this.Gender.Gender = gender;
			this.Category.Category = category;
		}
		
		public override string GetAnimalSpecificData()
		{
			return string.Format("{0}, {1}, {2}, {3}, {4}", ID, Name, Gender, Category, Age);
		}
		
		public override string ReturnInfo()
		{
			return string.Format("{0} claws and {1} whiskers", totalClaws, totalWhiskers);
		}
	}
}



Mammal:

using System;
using System.Collections.Generic;

namespace ApuAnimalMotel.AnimalTypes
{
	/// <summary>
	/// Description of Mammal.
	/// </summary>
	public abstract class Mammal : Animal
	{
		int clawAmount = 5;
		int legAmount = 4; 
		
		public Mammal()
		{
			
		}
		
		public int ClawAmount
		{
			get {return clawAmount;}
			set {clawAmount = value;}
		}
		
		public int LegAmount
		{
			get {return legAmount;}
			
			set {legAmount = value;}
		}
	}
}



Animal:

using System;
using System.Collections.Generic;

namespace ApuAnimalMotel
{
	/// <summary>
	/// Description of Animal.
	/// </summary>
	public abstract class Animal : IAnimal
	{	
		public int ID {get; set;}
		public string Name {get; set;}
		public GenderType Gender {get; set;}
		public int Age {get; set;}
		public CategoryType Category {get; set;}
		public string SpecialInfo;
		
		public Animal()
		{
			Gender = new GenderType();
			Category = new CategoryType();
		}
		
		public virtual string GetAnimalSpecificData()
		{
			return string.Format("{0}, {1}, {2}, {3}, {4}", ID, Name, Gender, Category, Age);
		}
		
		public virtual string ReturnInfo()
		{
			return SpecialInfo;
		}
	}
}



I also use two classes for Category and Gender:

CategoryType:

using System;
using System.Collections.Generic;

namespace ApuAnimalMotel
{
	/// <summary>
	/// Description of CategoryType.
	/// </summary>
	public class CategoryType
	{	
		string category;
		
		public string Category
		{
			get {return category;}
			
			set {category = value;}
		}
		
		public CategoryType()
		{
			
		}		
		
		public CategoryType(string category)
		{
			this.category = category;
		}
	}
}



GenderType:

using System;

namespace ApuAnimalMotel
{
	/// <summary>
	/// Description of GenderType.
	/// </summary>
	public class GenderType
	{
		string gender;
		
		public GenderType()
		{
			
		}
		
		public GenderType(string gender)
		{
			this.gender = gender;
		}
		
		public string Gender
		{
			get {return gender;}
			
			set {gender = value;}
		}
	}
}



Yeah, it's a pretty typical school assignment, but I can't find the error. I would've had a clue if like only one of the datas in ListBox came out wrong. Help greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: ListBox won't take in my items correctly

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: ListBox won't take in my items correctly

Posted 19 September 2013 - 07:15 PM

We are NOT going to do your debugging for you. Certainly not scrubbing through hundreds of lines of your homework code. You get graded on your skill not ours and that includes debugging skills.
There are numerous tutorials here on debugging. Check the tutorials section.

Quote

'0, , GenderType, CategoryType, 0' in the ListBox, every time.


Then you are sending an object to the listbox that doesn't know how to provide a decent .ToString(). That's what the listbox wants to show: The string of your type. You probably need to make a publically visible .ToString() method that will show the info you want in the way you want it. For example a 'person' class might show "lastname, firstname, age" as a single string even when those three items are separate properties.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3535
  • View blog
  • Posts: 10,943
  • Joined: 05-May 12

Re: ListBox won't take in my items correctly

Posted 19 September 2013 - 08:27 PM

Actually, he doesn't need the ToString() method because he is populating his listbox the ugly way:
animalsBox.Items.Add(animalList[i].GetAnimalSpecificData());



If he had overridden ToString(), then he could have done things the more object oriented and prettier way of:
animalsBox.Items.Add(animalList[i]);



The reason for his zero IDs and ages and blank name is due to a logic error in his Cat constructor. Obviously, he failed to step through his code to attempt to debug the situation.

And lastly, the reason for his category and gender types appearing as "CategoryType" and "GenderType" is because of
return string.Format("{0}, {1}, {2}, {3}, {4}", ID, Name, Gender, Category, Age);



Of course if only he was paying attention to how he initialized these two classes in his constructor, he could have retrieved the values the same way. Or alternately, he could have implemented ToString() on his Category and Gender classes to again get the more object oriented and prettier way.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: ListBox won't take in my items correctly

Posted 20 September 2013 - 05:57 PM

Well.. Its WinForms... So the whole approach is ugly.

If the OP would leave WinForm in the past and go to the more modern way that MS is pushing (WPF) then he could just do a binding to an ObservableCollection<string> and the GUI would update automatically as the collection updated.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3535
  • View blog
  • Posts: 10,943
  • Joined: 05-May 12

Re: ListBox won't take in my items correctly

Posted 20 September 2013 - 07:19 PM

Or even better, an ObservableCollection<IAnimal>.

Sent from my MT7001 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1