locked
Error: Namespace already contains a definition for.... Visual C# 2010

    Question

  • I've added a class by creating a code file named GradeBookTest.cs, when I try to run my application it is giving me this error: The namespace'<global namespace> already contains a definition for 'GradeBookTest'.

    Here is my code - I am not sure how I can correct it?

    pulic class GradeBookTest

    {

     //Main method begins program execution

           public static void Main(string[] args)

             // create a GradeBook object and assin it to myGradeBook

             GradeBook myGradeBook = new GradeBook();

             //call myGradeBook's displayMessage method

             myGradeBook.DisplayMessage();

    } // end main


    • Edited by Belkyn Saturday, September 29, 2012 6:36 PM
    Saturday, September 29, 2012 6:10 PM

Answers

  • Hi Belkyn,

      If you want to define the same class name at the same namespace, you need to define the partial key word at the beginning of the class name when defining it. It has been announced inside Partial Class Definitions (C# Programming Guide).

      Hope it helps you.

     


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, October 02, 2012 7:54 AM

All replies

  • Hello, looks like Class GradeBookTest defined multiple times in same namespace, can you search for this class and see if it is present in multiple times in same namespace

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    Saturday, September 29, 2012 6:16 PM
  • This may sound really dumb but... how do I do that?

    Belkyn

    Saturday, September 29, 2012 6:21 PM
  • Open code in editor, go to top of code, press Control-F. In the dialog box that appears type GradeBook and look for where it occurs.

    I'd guess that you will find two places where it is declared as a class. I think the one containing main is probably wrong - it usually defaults to Program.

    BTW the title of the post does not say anything about what the problem is so anyone with a similar problem will probably fail to find it. Perhaps you could change it to some like 'Namespace already contains definition' since that's the error you are trying to fix.

    Oops: does not have Main in class GradeBook but in GradeBookTest and that seems OK.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.


    • Edited by Riced Saturday, September 29, 2012 6:42 PM Added Oops
    Saturday, September 29, 2012 6:31 PM
  • Hello, Ctrl F in your .cs file and search for the class GradeBookTest 

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    Saturday, September 29, 2012 6:35 PM
  • So search within the same code? or search within the console period?  Yes, I have it written twice but I thought once you commented out (//) in the end the console will not read it?  I searched that class 'GradeBookTest' to find all references and it did bring back two results for that class.  So should I remove one of them and if so how? 

    C:\Users\bnmachado\Documents\Visual Studio 2010\Projects\4.2 Assignment.cs - (3, 14) : public class GradeBookTest

    C:\Users\bnmachado\documents\visual studio 2010\Projects\4.2 assignment\4.2 assignment\GradeBookTest1.cs - (3, 14) : public class GradeBookTest


    Belkyn

    Saturday, September 29, 2012 6:54 PM
  • Hi Belkyn,

      If you want to define the same class name at the same namespace, you need to define the partial key word at the beginning of the class name when defining it. It has been announced inside Partial Class Definitions (C# Programming Guide).

      Hope it helps you.

     


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, October 02, 2012 7:54 AM
  • This class GradeBookTest(both classes) will have the same methods? If so, why dont you make just one to be commom between your pages/console or whatever is it ?

    Web Developer

    Tuesday, October 02, 2012 12:31 PM