Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C#_Error "Namespace cannot directly contain..."

C#_Error "Namespace cannot directly contain..."

Scheduled Pinned Locked Moved C#
csharphelplinqquestion
17 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Deborah Palmer McCain

    As I am new at this, and this is my first attempt at actually writing code, some of you may chuckle (or laugh out loud at the simplistic code)... please be merciful. I keep getting the error message: A namespace cannot directly contain members such as fields or methods" I have visited MSDN, and virtually every google and bing hit out there, but no matter how I change the first entry, I get the same message. Could someone help?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace _11012_debproject_2
    {

    class Program
    
    {
        static void Main(string\[\] args);
    
    
    
    
    
          int number1;
          int number2;
          int Multiply;
    
    
    
     // prompt user
     // enter first number
    

    }

              Console.WriteLine( "Enter first number:" )
              number1 (Console.Readline());
     // prompt user
     // enter second number
    
              Console.WriteLine( "Enter second number:")
              number2 ( Console.ReadLine());
    
              product = number1 \* number2;
    
     // multiply numbers
    
           Console.WriteLine( "product is {0}",product );
    

    }
    // display product} }

    R Offline
    R Offline
    rajeevcapgeminiindia
    wrote on last edited by
    #3

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace _11012_debproject_2
    {

    class Program
    {
        static void Main(string\[\] args);
        {
          int number1;
          int number2;
          int Multiply;
    
          // prompt user
          // enter first number
          Console.WriteLine( "Enter first number:" );
          number1=Convert.ToInt32(Console.Readline());
          // prompt user
          // enter second number
          Console.WriteLine( "Enter second number:");
          number2=Convert.ToIn32( Console.ReadLine());
    
          Multiply= number1 \* number2;
    
          // multiply numbers
    
             Console.WriteLine( "Multiply is {0}    ",Multiply);
        }
    }
    

    }

    D 2 Replies Last reply
    0
    • D Deborah Palmer McCain

      As I am new at this, and this is my first attempt at actually writing code, some of you may chuckle (or laugh out loud at the simplistic code)... please be merciful. I keep getting the error message: A namespace cannot directly contain members such as fields or methods" I have visited MSDN, and virtually every google and bing hit out there, but no matter how I change the first entry, I get the same message. Could someone help?

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace _11012_debproject_2
      {

      class Program
      
      {
          static void Main(string\[\] args);
      
      
      
      
      
            int number1;
            int number2;
            int Multiply;
      
      
      
       // prompt user
       // enter first number
      

      }

                Console.WriteLine( "Enter first number:" )
                number1 (Console.Readline());
       // prompt user
       // enter second number
      
                Console.WriteLine( "Enter second number:")
                number2 ( Console.ReadLine());
      
                product = number1 \* number2;
      
       // multiply numbers
      
             Console.WriteLine( "product is {0}",product );
      

      }
      // display product} }

      R Offline
      R Offline
      RobCroll
      wrote on last edited by
      #4

      As mentioned, your } brackets are wrong and the problems starts with the ; at the end of your Main method.

      namespace _11012_debproject_2
      {

      class Program
      {
          static void Main(string\[\] args) // don't want ; here
          {
                int number1;
                int number2;
                int multiply; //better using camel case not title case
      
                // prompt user
                // enter first number
                Console.WriteLine( "Enter first number:" ); //you needed ; here
                number1 (Console.Readline());
      
                // prompt user
                // enter second number
                Console.WriteLine( "Enter second number:") //And here as well
                number2 ( Console.ReadLine());
      
                product = number1 \* number2;
      
                // multiply numbers
                Console.WriteLine( "product is {0}",product );
            
               //More stuff not provided in post
      
         } // End of Main method
      } //End of class
      

      } //End of namesspace

      You would do well to read a bit about naming conventions. namespaces should start with a letter. Not a number of underscore. Enjoy

      "You get that on the big jobs."

      D 1 Reply Last reply
      0
      • D Deborah Palmer McCain

        As I am new at this, and this is my first attempt at actually writing code, some of you may chuckle (or laugh out loud at the simplistic code)... please be merciful. I keep getting the error message: A namespace cannot directly contain members such as fields or methods" I have visited MSDN, and virtually every google and bing hit out there, but no matter how I change the first entry, I get the same message. Could someone help?

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;

        namespace _11012_debproject_2
        {

        class Program
        
        {
            static void Main(string\[\] args);
        
        
        
        
        
              int number1;
              int number2;
              int Multiply;
        
        
        
         // prompt user
         // enter first number
        

        }

                  Console.WriteLine( "Enter first number:" )
                  number1 (Console.Readline());
         // prompt user
         // enter second number
        
                  Console.WriteLine( "Enter second number:")
                  number2 ( Console.ReadLine());
        
                  product = number1 \* number2;
        
         // multiply numbers
        
               Console.WriteLine( "product is {0}",product );
        

        }
        // display product} }

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #5

        The problem (as mentioned above) is your curly braces - the last two are commented out, as is should by them being coloured green. If you get this kind of problem, Try CTRL+K CTRL+D - it formats your code so that everything is indented nicely. It won't work when you have missing braces, but if you fix that and try it, it should be obvious in future where things are going wrong. BTW: You can change how Visual Studio handles your indentation to your preferred style in "Tools...Options" and then "TextEditor", "C#", "Formatting", "Indentation". So if you prefer 1TBS:

        if (condition) {
        statement;
        statement;
        }

        Or K&R:

        if (condition)
        {
        statement;
        statement;
        }

        Or Whitesmiths:

        if (condition)
        {
        statement;
        statement;
        }

        You can get it to work with it happily. (I prefer Whitesmiths)

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        D K 2 Replies Last reply
        0
        • R Roger Wright

          Check your curly braces... they don't properly enclose the object.

          Will Rogers never met me.

          D Offline
          D Offline
          Deborah Palmer McCain
          wrote on last edited by
          #6

          Roger: Thank you. I appreciate your help.

          1 Reply Last reply
          0
          • R rajeevcapgeminiindia

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;

            namespace _11012_debproject_2
            {

            class Program
            {
                static void Main(string\[\] args);
                {
                  int number1;
                  int number2;
                  int Multiply;
            
                  // prompt user
                  // enter first number
                  Console.WriteLine( "Enter first number:" );
                  number1=Convert.ToInt32(Console.Readline());
                  // prompt user
                  // enter second number
                  Console.WriteLine( "Enter second number:");
                  number2=Convert.ToIn32( Console.ReadLine());
            
                  Multiply= number1 \* number2;
            
                  // multiply numbers
            
                     Console.WriteLine( "Multiply is {0}    ",Multiply);
                }
            }
            

            }

            D Offline
            D Offline
            Deborah Palmer McCain
            wrote on last edited by
            #7

            Thank you for your help. I appreciate your time.

            1 Reply Last reply
            0
            • R RobCroll

              As mentioned, your } brackets are wrong and the problems starts with the ; at the end of your Main method.

              namespace _11012_debproject_2
              {

              class Program
              {
                  static void Main(string\[\] args) // don't want ; here
                  {
                        int number1;
                        int number2;
                        int multiply; //better using camel case not title case
              
                        // prompt user
                        // enter first number
                        Console.WriteLine( "Enter first number:" ); //you needed ; here
                        number1 (Console.Readline());
              
                        // prompt user
                        // enter second number
                        Console.WriteLine( "Enter second number:") //And here as well
                        number2 ( Console.ReadLine());
              
                        product = number1 \* number2;
              
                        // multiply numbers
                        Console.WriteLine( "product is {0}",product );
                    
                       //More stuff not provided in post
              
                 } // End of Main method
              } //End of class
              

              } //End of namesspace

              You would do well to read a bit about naming conventions. namespaces should start with a letter. Not a number of underscore. Enjoy

              "You get that on the big jobs."

              D Offline
              D Offline
              Deborah Palmer McCain
              wrote on last edited by
              #8

              I am having difficulty posting replies. If this is a double, I apologize. Thank you for your assistance. My namespace "name" is a bad habit I must break (from business emails with the date at the beginning of the Subject line). Thank you for the reminder. Also, the braces? I have no excuse for the carelessness.

              R 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                The problem (as mentioned above) is your curly braces - the last two are commented out, as is should by them being coloured green. If you get this kind of problem, Try CTRL+K CTRL+D - it formats your code so that everything is indented nicely. It won't work when you have missing braces, but if you fix that and try it, it should be obvious in future where things are going wrong. BTW: You can change how Visual Studio handles your indentation to your preferred style in "Tools...Options" and then "TextEditor", "C#", "Formatting", "Indentation". So if you prefer 1TBS:

                if (condition) {
                statement;
                statement;
                }

                Or K&R:

                if (condition)
                {
                statement;
                statement;
                }

                Or Whitesmiths:

                if (condition)
                {
                statement;
                statement;
                }

                You can get it to work with it happily. (I prefer Whitesmiths)

                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                D Offline
                D Offline
                Deborah Palmer McCain
                wrote on last edited by
                #9

                Thank you very much for the guidance. I will make the necessary adjustment in Visual Studio. I appreciate you taking the time to help.

                1 Reply Last reply
                0
                • R rajeevcapgeminiindia

                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;

                  namespace _11012_debproject_2
                  {

                  class Program
                  {
                      static void Main(string\[\] args);
                      {
                        int number1;
                        int number2;
                        int Multiply;
                  
                        // prompt user
                        // enter first number
                        Console.WriteLine( "Enter first number:" );
                        number1=Convert.ToInt32(Console.Readline());
                        // prompt user
                        // enter second number
                        Console.WriteLine( "Enter second number:");
                        number2=Convert.ToIn32( Console.ReadLine());
                  
                        Multiply= number1 \* number2;
                  
                        // multiply numbers
                  
                           Console.WriteLine( "Multiply is {0}    ",Multiply);
                      }
                  }
                  

                  }

                  D Offline
                  D Offline
                  Deborah Palmer McCain
                  wrote on last edited by
                  #10

                  I wanted to wander back and thank you formally. I changed "product" to "muliply" and it looks like I neglected the "convert to 32" as well as my other errors. Thank you again.

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    The problem (as mentioned above) is your curly braces - the last two are commented out, as is should by them being coloured green. If you get this kind of problem, Try CTRL+K CTRL+D - it formats your code so that everything is indented nicely. It won't work when you have missing braces, but if you fix that and try it, it should be obvious in future where things are going wrong. BTW: You can change how Visual Studio handles your indentation to your preferred style in "Tools...Options" and then "TextEditor", "C#", "Formatting", "Indentation". So if you prefer 1TBS:

                    if (condition) {
                    statement;
                    statement;
                    }

                    Or K&R:

                    if (condition)
                    {
                    statement;
                    statement;
                    }

                    Or Whitesmiths:

                    if (condition)
                    {
                    statement;
                    statement;
                    }

                    You can get it to work with it happily. (I prefer Whitesmiths)

                    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                    K Offline
                    K Offline
                    killabyte
                    wrote on last edited by
                    #11

                    K&R is for real programmers good sir... :cool:

                    OriginalGriffO 1 Reply Last reply
                    0
                    • D Deborah Palmer McCain

                      I am having difficulty posting replies. If this is a double, I apologize. Thank you for your assistance. My namespace "name" is a bad habit I must break (from business emails with the date at the beginning of the Subject line). Thank you for the reminder. Also, the braces? I have no excuse for the carelessness.

                      R Offline
                      R Offline
                      Roger Wright
                      wrote on last edited by
                      #12

                      Deborah Palmer McCain wrote:

                      I am having difficulty posting replies.

                      Don't worry about that, Deborah. Quite a few of us have been having trouble with that for the past week or so. The site administrators are running some SQL Server scripts that are processor-intensive, and that tends to bog things down a bit. Be patient... It will get better.

                      Will Rogers never met me.

                      D 1 Reply Last reply
                      0
                      • K killabyte

                        K&R is for real programmers good sir... :cool:

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #13

                        Nah. It's clumsy and inconsistent with single statement indentation: If you write like this:

                        if (condition)
                        statement;

                        Why would you change the indentation when it is a block you are using instead of a single statement?

                        if (condition)
                        {
                        statement;
                        statement;
                        }

                        It's up to personal preference (or company practice) though - the important thing is to be consistent throughout.

                        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        B B 2 Replies Last reply
                        0
                        • R Roger Wright

                          Deborah Palmer McCain wrote:

                          I am having difficulty posting replies.

                          Don't worry about that, Deborah. Quite a few of us have been having trouble with that for the past week or so. The site administrators are running some SQL Server scripts that are processor-intensive, and that tends to bog things down a bit. Be patient... It will get better.

                          Will Rogers never met me.

                          D Offline
                          D Offline
                          Deborah Palmer McCain
                          wrote on last edited by
                          #14

                          Roger: Thank you for the update, I thought that 7 was wreaking havoc with my one lonely desktop. I used Vista (no gagging) with relish for so many years, that the Apple-esque features of 7 cause me to blame it for everything. I do like Stick Notes though. Just as an update, my application runs smoothly after I fixed further code errors Thank you again for your help. Deborah

                          1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            Nah. It's clumsy and inconsistent with single statement indentation: If you write like this:

                            if (condition)
                            statement;

                            Why would you change the indentation when it is a block you are using instead of a single statement?

                            if (condition)
                            {
                            statement;
                            statement;
                            }

                            It's up to personal preference (or company practice) though - the important thing is to be consistent throughout.

                            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                            B Offline
                            B Offline
                            BillWoodruff
                            wrote on last edited by
                            #15

                            I prefer K&R to 1TBS (devotees of both cults can be fanatics), because I think it's more readable, and more easily maintainable. WhiteSmith's I've never personally seen used at any place I've worked. I maintain two simple macros in UltraEdit that will convert K&R<=>1TBS, if I should ever need to. But, I think your "shibboleth" example code is a bit mis-leading: nothing stops you from using "pure" K&S and writing:

                            if(condition}
                            {
                            statement;
                            }

                            Or even:

                            if (condition) { statement; }

                            I've asked the braces directly how they feel about having only one statement, and they assure me they do not feel neglected or lonely :) I think lack of braces around one simple and short statement, following an if-clause is seldom written with braces no matter what style of formatting you use. Where omitting the braces would really "bother" me would be when the the single statement following the if-clause was so bloody long that it overflowed the text-working-area boundaries to be several lines long ! But, of course, you are, as usual, bulls-eye-center, when you emphasize you may not have personal choice if you go to work on some project where there are code formatting guidelines in place for all team members. Thank goodness C# does not allow multiple statements separated by colons, or "line-continuation" characters, as in the late Pleistocene when VB roamed the savannas, and real men did not eat quiche and programmed only in C and assembly language. best, Bill

                            "It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle

                            OriginalGriffO 1 Reply Last reply
                            0
                            • B BillWoodruff

                              I prefer K&R to 1TBS (devotees of both cults can be fanatics), because I think it's more readable, and more easily maintainable. WhiteSmith's I've never personally seen used at any place I've worked. I maintain two simple macros in UltraEdit that will convert K&R<=>1TBS, if I should ever need to. But, I think your "shibboleth" example code is a bit mis-leading: nothing stops you from using "pure" K&S and writing:

                              if(condition}
                              {
                              statement;
                              }

                              Or even:

                              if (condition) { statement; }

                              I've asked the braces directly how they feel about having only one statement, and they assure me they do not feel neglected or lonely :) I think lack of braces around one simple and short statement, following an if-clause is seldom written with braces no matter what style of formatting you use. Where omitting the braces would really "bother" me would be when the the single statement following the if-clause was so bloody long that it overflowed the text-working-area boundaries to be several lines long ! But, of course, you are, as usual, bulls-eye-center, when you emphasize you may not have personal choice if you go to work on some project where there are code formatting guidelines in place for all team members. Thank goodness C# does not allow multiple statements separated by colons, or "line-continuation" characters, as in the late Pleistocene when VB roamed the savannas, and real men did not eat quiche and programmed only in C and assembly language. best, Bill

                              "It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle

                              OriginalGriffO Offline
                              OriginalGriffO Offline
                              OriginalGriff
                              wrote on last edited by
                              #16

                              Personally, I use braces round single statements, but then I grew up without auto-indenting IDEs, so it was too easy to get caught by assuming logic flow from the indentation:

                              if (condition)
                              statement;
                              statement;

                              Which is one of the reasons I really dislike 1TBS: it can be far to easy to miss the opening bracket, particularly if the condition is quite long.

                              Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                              1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                Nah. It's clumsy and inconsistent with single statement indentation: If you write like this:

                                if (condition)
                                statement;

                                Why would you change the indentation when it is a block you are using instead of a single statement?

                                if (condition)
                                {
                                statement;
                                statement;
                                }

                                It's up to personal preference (or company practice) though - the important thing is to be consistent throughout.

                                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                                B Offline
                                B Offline
                                BobJanova
                                wrote on last edited by
                                #17

                                I prefer

                                if(condition) statement;

                                and

                                if(condition){
                                statement;
                                statement;
                                }

                                Only for a limited set of statement lengths (or a long condition) will I do

                                if(condition_which_is_quite_long)
                                statement;

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups