locked
Invalid token '(' in class RRS feed

  • Question

  • 1using System; 
    2 
    3 
    4namespace program1 
    5
    6    class Program 
    7    { 
    8        //this is where your program starts. 
    9        static void Main(string[] args) 
    10        { 
    11            //prompt user to enter a name. 
    12            Console.WriteLine("Enter your name please"); 
    13 
    14            //now read the name entered. 
    15            string name = Console.ReadLine(); 
    16        } 
    17 
    18        //Greet the user with the name. 
    19        Console.WriteLine("Hello, "namespace); 
    20 
    21        //wait for user to acknowledge the results. 
    22        Console.writeline("press enter to terminate..."
    23        Console.Read(); 
    24 
    Wednesday, February 18, 2009 6:21 AM

Answers

  • Well there's a couple of errors.
    You end your main() before at line 16. Everything behind that, is not in any method.
    Also, if this is your entire code, you're missing a } at the end, to end your class and one more to end your namespace.

    Try this instead:

    1using System;  
    2      
    3      
    4    namespace program1  
    5    {  
    6        class Program  
    7        {  
    8            //this is where your program starts.  
    9            static void Main(string[] args)  
    10            {  
    11                //prompt user to enter a name.  
    12                Console.WriteLine("Enter your name please");  
    13      
    14                //now read the name entered.  
    15                string name = Console.ReadLine();  
    16              
    17      
    18                //Greet the user with the name.  
    19                Console.WriteLine("Hello, "+ name);  
    20      
    21                //wait for user to acknowledge the results.  
    22                Console.writeline("press enter to terminate...");
    23                Console.Read();  
    24
    25
    26
    27  

    And what the others said.
    • Marked as answer by moron 101 Wednesday, February 18, 2009 6:01 PM
    Wednesday, February 18, 2009 7:05 AM
  •  The body is encased within the curly-braces. {}  Namespaces, classes, methods, and things like multi-line if() statements/loop structures, etc. all use curly-braces to denote the start and end of what is in their scope or body. In your example given you had the Console.WriteLine calls outside of the method "Main". Code cannot be executed like that outside of a method.

    If you had tried to compile that program the compiler would have barfed out error messages to that effect. In your case the intellisense picked it up though where it highlighted probably didn't make much sense. When in doubt, do a compile and go through the error messages.
    #2 happens.
    • Marked as answer by moron 101 Wednesday, February 18, 2009 6:01 PM
    Wednesday, February 18, 2009 8:03 AM

All replies

  • for some reason every "(" i put is Invalid...anyone know why?
    Wednesday, February 18, 2009 6:21 AM
  • There is missing ; in Console.Writeline("press enter to terminate...") line. (and writeline must be Writeline(case sesitive))

    SaltFish

    Salty and chilled, but always coded!
    Wednesday, February 18, 2009 7:04 AM
  • Well there's a couple of errors.
    You end your main() before at line 16. Everything behind that, is not in any method.
    Also, if this is your entire code, you're missing a } at the end, to end your class and one more to end your namespace.

    Try this instead:

    1using System;  
    2      
    3      
    4    namespace program1  
    5    {  
    6        class Program  
    7        {  
    8            //this is where your program starts.  
    9            static void Main(string[] args)  
    10            {  
    11                //prompt user to enter a name.  
    12                Console.WriteLine("Enter your name please");  
    13      
    14                //now read the name entered.  
    15                string name = Console.ReadLine();  
    16              
    17      
    18                //Greet the user with the name.  
    19                Console.WriteLine("Hello, "+ name);  
    20      
    21                //wait for user to acknowledge the results.  
    22                Console.writeline("press enter to terminate...");
    23                Console.Read();  
    24
    25
    26
    27  

    And what the others said.
    • Marked as answer by moron 101 Wednesday, February 18, 2009 6:01 PM
    Wednesday, February 18, 2009 7:05 AM
  • You have to place the lines 19-23 inside the body of a method like this:

    namespace program1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                //prompt user to enter a name.    
                Console.WriteLine("Enter your name please");  
     
                //now read the name entered.    
                string name = Console.ReadLine();  
                //Greet the user with the name.    
                Console.WriteLine("Hello, " + name);  
     
                //wait for user to acknowledge the results.    
                Console.WriteLine("press enter to terminate...");  
                Console.Read();  
            }  
        }  



    • Proposed as answer by Some_Crazy_Guy Wednesday, February 18, 2009 12:37 PM
    Wednesday, February 18, 2009 7:06 AM
  • what and where determines the body of the method?
    Wednesday, February 18, 2009 7:45 AM
  •  The body is encased within the curly-braces. {}  Namespaces, classes, methods, and things like multi-line if() statements/loop structures, etc. all use curly-braces to denote the start and end of what is in their scope or body. In your example given you had the Console.WriteLine calls outside of the method "Main". Code cannot be executed like that outside of a method.

    If you had tried to compile that program the compiler would have barfed out error messages to that effect. In your case the intellisense picked it up though where it highlighted probably didn't make much sense. When in doubt, do a compile and go through the error messages.
    #2 happens.
    • Marked as answer by moron 101 Wednesday, February 18, 2009 6:01 PM
    Wednesday, February 18, 2009 8:03 AM
  • got it, thanks!
    Wednesday, February 18, 2009 6:02 PM