locked
C#: does not contain a constructor that takes ( ) arguments

    Question

  • A newcommer's question:
    I made a small (incomplete) program and cannt get it to compile. I get the error:
    "Error 1 'Animals1.Zoo' does not contain a constructor that takes '4' arguments".
    I belive I made all constructors as I should, but still can't get it to run.
    What am I doing wrong?

    Here it is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Animals1
    {
        //This is the base class for all Animals
        abstract public class Zoo
        {
            //Defs:
    
            string AnimalName;
            int AnimalID;
            String AnimalCryOut;
            Decimal AverageWheight;
    
            // Default ctor.
            public Zoo() { }
    
            // Custom ctor
            public void Elephant(string AnimalName, int AnimalID, String AnimalCryOut, Decimal AverageWheight)
            {
                this.AnimalName = AnimalName;
                this.AnimalID = AnimalID;
                this.AnimalCryOut = AnimalCryOut;
                this.AverageWheight = AverageWheight;
            }
    
            public virtual void DisplayInfo()
            {
                Console.WriteLine("Name: {0}", AnimalName);
                Console.WriteLine("ID: {0}", AnimalID);
                Console.WriteLine("Shout: {0}", AnimalCryOut);
                Console.WriteLine("Wheight: {0}", AverageWheight);
                return;
            }
        }
        //======================
        class Elephant : Zoo
        {
            public Elephant() { }
    
            public Elephant(string AnimalName, int AnimalID, String AnimalCryOut, Decimal AverageWheight)
                : base(AnimalName, AnimalID, AnimalCryOut, AverageWheight)
            {
            }
    
            public override void DisplayInfo()
            {
                base.DisplayInfo();
                Console.WriteLine("Elephant Info");
            }
    
        }
    
        //==========================
        class Program
        {
            static void Main(string[] args)
            {
                Elephant Dumbo = new Elephant("Dumbo", 1001, "Ahooo...", 3400);
                //Snake Sleeky = new Snake("Sleeky", 2002, "Ahooo...", 2);
                //Fish Wanda = new Fish("Wanda", 3003, "", 12);
            }
    
        }
    }
    • Edited by Samtal Friday, July 31, 2009 7:11 AM
    Friday, July 31, 2009 7:03 AM

Answers

  • The Elephant constructor calls the Zoo constructor (base) with 4 arguments.  Zoo doesn't have such a constructor.  I assume you accidentally used "Elephant" instead of "Zoo" in the Zoo class.  You commented it // Custom ctor, it is not a ctor.  Constructors don't have a return type.

    Also consider the confusion your code might cause to a reader.  An Elephant is an Animal, not a Zoo.  A Zoo is a collection of Animals.

    Hans Passant.
    • Proposed as answer by Fernando Nicolet Friday, July 31, 2009 7:44 PM
    • Marked as answer by Harry Zhu Thursday, August 06, 2009 5:53 AM
    Friday, July 31, 2009 12:01 PM