Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Binding
I'm having some problem with this method of mine. Somehow, I doesn't create or doesn't accept the created object (animal). It seems it only return null.
 
public Animal GetSetAnimalInfo(string name, double age, CategoryType cat, object animalType, GenderType gender, string extra)
        {
            Animal animalObj = null;
 
            switch (cat)
            {
                case CategoryType.Bird:
                    Bird.BirdSpecies birdSpecie = (Bird.BirdSpecies)Enum.Parse(typeof(Bird.BirdSpecies), animalType.ToString());
                    animalObj = Bird.BirdFactory.CreateBird(birdSpecie); //late binding
                    break;
 
                case CategoryType.Insect:
                    Insect.InsectSpecies insectSpecie = (Insect.InsectSpecies)Enum.Parse(typeof(Insect.InsectSpecies), animalType.ToString());
                    animalObj = Insect.InsectFactory.CreateInsect(insectSpecie); //late binding
                    break;
 
                case CategoryType.Mammal:
                    Mammal.MammalSpecies mammalSpecie = (Mammal.MammalSpecies)Enum.Parse(typeof(Mammal.MammalSpecies), animalType.ToString());
                    animalObj = Mammal.MammalFactory.CreateMammal(mammalSpecie);
                    break;
 
                case CategoryType.Marine:
                    Marine.MarineSpecies marineSpecie = (Marine.MarineSpecies)Enum.Parse(typeof(Marine.MarineSpecies), animalType.ToString());
                    animalObj = Marine.MarineFactory.CreateMarine(marineSpecie);
                    break;
 
                case CategoryType.Reptile:
                    Reptile.ReptileSpecies reptileSpecie = (Reptile.ReptileSpecies)Enum.Parse(typeof(Reptile.ReptileSpecies), animalType.ToString());
                    animalObj = Reptile.ReptileFactory.CreateReptile(reptileSpecie);
                    break;
            }
 

            if (animalObj != null)
            {
                System.Windows.Forms.MessageBox.Show(animalType.ToString());
            }
 
            if (!InputUtility.IsLettersOnly(name))
            {
                string message = string.Format("Animal could not be registered because the name input was not correct.") + Environment.NewLine;
 
                throw new InvalidInputException(message);
            }
            else
            {
                animalObj.Name = name;
            }
 
            animalObj.ID = GenerateID(animalObj);
 
            if (!InputUtility.IsNumeric(age))
            {
                string message = string.Format("Animal could not be registered because the age input was not correct." + Environment.NewLine);
 
                throw new InvalidInputException(message);
            }
            else
            {
                animalObj.Age = age;
            }
 
            animalObj.Gender = gender;
 
            animalObj.ExtraAnimalInfo = extra;
 
            AddToList(animalObj);
 
            return animalObj;
        }
 
This is the second method, which is being used for the GUI whereas the first one handles the data, and creation of objects. However, as mentioned above, somehow the first method returns null
 
private Animal AddAnimal()
        {
            Animal animalObj = null;
            try
            {
                animalManager.GetSetAnimalInfo( txtName.Text,
                                                Convert.ToDouble(txtAge.Text),
                                                (CategoryType)lstCategory.SelectedIndex,
                                                lstAnimalType.SelectedValue,
                                                (GenderType)lstGender.SelectedIndex,
                                                txtSpecialInput.Text);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
 
            if (animalObj != null)
            {
                try
                {
                    ListViewItem lvi = new ListViewItem(animalObj.Name);
                    lvi.SubItems.Add(animalObj.ID);
                    lvi.SubItems.Add(animalObj.Age.ToString());
                    lvi.SubItems.Add(animalObj.Gender.ToString());
                    lvi.SubItems.Add(animalObj.Category.ToString());
                    lstVRegisteredAnimals.Items.Add(lvi);
 
                }
 

                catch (FormatException ex) { MessageBox.Show(ex.Message); }
            }
            return animalObj;
        }
Posted 4-Mar-13 13:46pm
Comments
Sergey Alexandrovich Kryukov at 4-Mar-13 19:06pm
   
Can I unsee it? :-)
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

First, I agree that you are no using OOP, but let's focus on your problem.
 
You do:
 
Animal animalObj = null;
 

But you never do:
animalObj = something...
 

I think your animalManager.GetSetAnimalInfo should look more like:
animalObj = animalManager.GetSetAnimalInfo.
 
After all, your animalManager is returning an object that is lost, and then you are reading the animalObj variable.
 
That was the part 1. Simple solving your problem about animal being null.
 
But about the switch.
Instead of an enum (CategoryType) and a switch (over the cat variable) you could have a CategoryDelegate (to say that it is a categorytype, but that executes some code), an ICategoryType or something.
Then, each value of the switch is a new entire type, which implements a method (like CreateValue), without requiring a switch.
 
If you need help with that, I could give real examples... now I am simple trying to answer it fast.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please do, yourself a big favor, stop all of this activity. A switch statement by the type is something directly opposite to OOP, a total abuse. Actually, roughly speaking, the main point of OOP was to avoid such things.
 
Nothing can help you to fix your code, just because the whole idea is wrong. It just makes no sense. Also, I don't see a reason to explain you how OOP works. Read anything explaining OOP; and I hope you will see what to do. Or not. Frown | :-(
 
[EDIT]
 
OK, a hint: whatever you have under each case should be a virtual method overridden in each concrete (non-abstract) class. Then some method using it should be one line: a call to the same method of an abstract class, as a compile-time type. As a run-time will be some concrete type, the call will be dispatched to one of the overridden methods, through late binding.
 
If you already using the term "late binding", use late binding.
As one Easter proverb say, "Saying "halva-halva" won't make your mouth sweet". Smile | :)
 
—SA
  Permalink  
v2
Comments
leprechauny at 4-Mar-13 19:21pm
   
Well, this is one of the methods that are being called under one of the cases. I don't see why you don't qualify it as late binding?
 
public static Bird CreateBird(BirdSpecies Species)
{
Bird animalObj = null; //Unknown at this time

//Late binding
switch (Species)
{
case BirdSpecies.Eagle:
animalObj = new Eagle();
break;
 
case BirdSpecies.Pelican:
animalObj = new Pelican();
break;
 
default:
Debug.Assert(false, "To be filled.");
break;
}
 
//Sets category
animalObj.Category = CategoryType.Bird;
 
//Returns the created object
return animalObj;
}
Sergey Alexandrovich Kryukov at 4-Mar-13 19:31pm
   
All wrong again. Forgot all your cases, finally! Makes no sense at all...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100