[RESOLVED] Inheritance namespace already contains definition error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Inheritance namespace already contains definition error

  1. #1
    Join Date
    Jun 2013
    Posts
    3

    [RESOLVED] Inheritance namespace already contains definition error

    Hi All,

    I'm years out of practice and started to re-learn programming. I thought a fun way to learn inheritance is have the user choose a species and a list of sub-species will appear and so on and so forth.

    I had my base and derived class in the same file as my opening form but then it complained I can only have 1 partial class in a file so I split it into a second .cs file. Now I get
    Error The namespace '<global namespace>' already contains a definition for 'kelf'
    when I try to run the program.

    I ran the search and it only found 4 instances of kelf (all listed in the same new Fox.cs file).

    Fox.cs
    Code:
    public abstract class kelf
    {
        private string name;
        private string species;
        private int gestPeriod;
    
        public kelf(string species)
        {
            this.species = species;
        }
    
        ~kelf()
        {
    
        }
    
        public string GetSpecies()
        {
            return this.species;
        }
    
    }
    public class kelf : Foxes
    {
        /*public Fox()
        {
    
        }
        public void test()
        {
            messagebox(this.species);
        }*/
    }
    form1.cs
    Code:
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Inheritance
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if(cmbAnimals.Text == "")
                {
                    MessageBox.Show("You must choose a top level species");
                }
                else
                {
                    //Animal tiger = new Animal(lblAnimal.Text);
                }
    
            }
    
            private void cmbAnimals_SelectedIndexChanged(object sender, EventArgs e)
            {
                switch(cmbAnimals.Text.ToUpper())
                {
                    case "CANIDAE": lblAnimal.Text = "FOX chosen";
                        //Fox newFox = new Fox();
                    break;
                    case "GIRAFFA CAMELOPARDALIS": lblAnimal.Text = "Giraffa camelopardalis";
                    break;
                    case "PANTHERA TIGRIS": lblAnimal.Text = "Panthera tigris";
                    break;
                    default: break;
                }
            }
    
        }
    
    }
    In the solution Explorer my Fox.cs is a direct sub-node of the solution if that makes any difference.
    So it goes

    Solution
    -Properties
    -References
    -Form1.cs
    --Form1.Designer.cs
    --Form1.resx
    -Fox.cs
    -Program.cs

    I tried looking up this error and it says my project has this class name somewhere else but I ran the search and it only found the 4 cases there. I checked syntax for Inheritance and it seemed correct to me. I'm not sure what I did wrong so any suggestions would be great/

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: Inheritance namespace already contains definition error

    Reading the code and observing the ".cs" file extension, I guess that this is C# code, not C++ code written for Microsoft Visual C++.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,243

    Re: Inheritance namespace already contains definition error

    I've moved the thread into the C# forum.

    The problem is simple - you've declared an abstract kelp class and then another kelp class that is derived from a Foxes class. You've simply reversed the order of the derived class declaration.

    So instead of
    Code:
    public class kelf : Foxes
    {
      ...
    }
    you should have..
    Code:
    public class Foxes : kelf
    {
      ...
    }
    Btw, generally the names of classes in C# are declared PascalCased, so the name kelf should be Kelf if you follow the usual convention.

  4. #4
    Join Date
    Jun 2013
    Posts
    3

    Re: Inheritance namespace already contains definition error

    Well originally it was Foxes but I changed the name on the 4 cases to see if it made any difference at some point during my struggles lol.

    Thanks so much! So easy and I looked http://msdn.microsoft.com/en-us/libr...ce(VS.90).aspx for so long trying to figure it out and it was staring at me the whole time.

    Much, much appreciated!

  5. #5
    Join Date
    Jun 2013
    Posts
    3

    Re: [RESOLVED] Inheritance namespace already contains definition error

    I meant originally kelf was named Animals. My original reply is awaiting approval but I noticed the mistake after hitting submit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center