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. Errors with book example program

Errors with book example program

Scheduled Pinned Locked Moved C#
csharphelptutorialquestionlearning
8 Posts 5 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.
  • L Offline
    L Offline
    Lurker00
    wrote on last edited by
    #1

    Trying to run the following console program from a 'Sam's' Learn C# book and getting a bunch of errors; can anyone see what might be causing these please? (I'm only a C# rookie myself.) Error 1 A namespace does not directly contain members such as fields or methods Error 3 Invalid token '7' in class, struct, or interface member declaration Error 4 Invalid token '9' in class, struct, or interface member declaration Error 5 Invalid token '10' in class, struct, or interface member declaration Error 6 Invalid token '11' in class, struct, or interface member declaration Error 7 Invalid token '12' in class, struct, or interface member declaration 1: // Circle1.cs - Overloading the area method 2: //-------------------------------------------------------------------- 3: 4: using System; 5: 6: public class Circle 7: { 8: public int x; 9: public int y; 10: public double radius; 11: private const float PI = 3.14159F; 12: 13: public double Area() // Uses values from data members 14: { 15: return Area(radius); 16: } 17: 18: public double Area( double rad ) 19: { 20: double theArea; 21: theArea = PI * rad * rad; 22: Console.WriteLine(" The area for radius ({0}) is {1}", rad, theArea); 23: return theArea; 24: } 25: 26: public double Area(int x1, int y1, double rad) 27: { 28: return Area(rad); 29: } 30: 31: public double Area( int x1, int y1, int x2, int y2 ) 32: { 33: int x_diff; 34: int y_diff; 35: double rad; 36: 37: x_diff = x2 - x1; 38: y_diff = y2 - y1; 39: 40: rad = (double) Math.Sqrt((x_diff * x_diff) + (y_diff * y_diff)); 41: 42: return Area(rad); 43: } 44: 45: public Circle() 46: { 47: x = 0; 48: y = 0; 49: radius = 0.0; 50: } 51: } 52: 53: class CircleApp 54: { 55: public static void Main() 56: { 57: Circle myCircle = new Circle(); 58: 59: Console.WriteLine("Passing nothing..."); 60: myCircle.Area(); 61: 62: Console.WriteLine("\nPassing a radius of 3..."); 63: myCircle.Area( 3 ); 64: 65: Console.WriteLine("\nPassing a center of (2, 4) and a radius of 3..."); 66: myCircle.Area( 2, 4, 3 ); 67: 68: Console.WriteLine("\nPassing center of (2, 3) and a point of (5, 6)..."); 69: myCircle.Area( 2, 3,

    C S B 3 Replies Last reply
    0
    • L Lurker00

      Trying to run the following console program from a 'Sam's' Learn C# book and getting a bunch of errors; can anyone see what might be causing these please? (I'm only a C# rookie myself.) Error 1 A namespace does not directly contain members such as fields or methods Error 3 Invalid token '7' in class, struct, or interface member declaration Error 4 Invalid token '9' in class, struct, or interface member declaration Error 5 Invalid token '10' in class, struct, or interface member declaration Error 6 Invalid token '11' in class, struct, or interface member declaration Error 7 Invalid token '12' in class, struct, or interface member declaration 1: // Circle1.cs - Overloading the area method 2: //-------------------------------------------------------------------- 3: 4: using System; 5: 6: public class Circle 7: { 8: public int x; 9: public int y; 10: public double radius; 11: private const float PI = 3.14159F; 12: 13: public double Area() // Uses values from data members 14: { 15: return Area(radius); 16: } 17: 18: public double Area( double rad ) 19: { 20: double theArea; 21: theArea = PI * rad * rad; 22: Console.WriteLine(" The area for radius ({0}) is {1}", rad, theArea); 23: return theArea; 24: } 25: 26: public double Area(int x1, int y1, double rad) 27: { 28: return Area(rad); 29: } 30: 31: public double Area( int x1, int y1, int x2, int y2 ) 32: { 33: int x_diff; 34: int y_diff; 35: double rad; 36: 37: x_diff = x2 - x1; 38: y_diff = y2 - y1; 39: 40: rad = (double) Math.Sqrt((x_diff * x_diff) + (y_diff * y_diff)); 41: 42: return Area(rad); 43: } 44: 45: public Circle() 46: { 47: x = 0; 48: y = 0; 49: radius = 0.0; 50: } 51: } 52: 53: class CircleApp 54: { 55: public static void Main() 56: { 57: Circle myCircle = new Circle(); 58: 59: Console.WriteLine("Passing nothing..."); 60: myCircle.Area(); 61: 62: Console.WriteLine("\nPassing a radius of 3..."); 63: myCircle.Area( 3 ); 64: 65: Console.WriteLine("\nPassing a center of (2, 4) and a radius of 3..."); 66: myCircle.Area( 2, 4, 3 ); 67: 68: Console.WriteLine("\nPassing center of (2, 3) and a point of (5, 6)..."); 69: myCircle.Area( 2, 3,

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      I can't see it. You are going to have to provide the line numbers that the compiler is giving.


      Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

      L 1 Reply Last reply
      0
      • L Lurker00

        Trying to run the following console program from a 'Sam's' Learn C# book and getting a bunch of errors; can anyone see what might be causing these please? (I'm only a C# rookie myself.) Error 1 A namespace does not directly contain members such as fields or methods Error 3 Invalid token '7' in class, struct, or interface member declaration Error 4 Invalid token '9' in class, struct, or interface member declaration Error 5 Invalid token '10' in class, struct, or interface member declaration Error 6 Invalid token '11' in class, struct, or interface member declaration Error 7 Invalid token '12' in class, struct, or interface member declaration 1: // Circle1.cs - Overloading the area method 2: //-------------------------------------------------------------------- 3: 4: using System; 5: 6: public class Circle 7: { 8: public int x; 9: public int y; 10: public double radius; 11: private const float PI = 3.14159F; 12: 13: public double Area() // Uses values from data members 14: { 15: return Area(radius); 16: } 17: 18: public double Area( double rad ) 19: { 20: double theArea; 21: theArea = PI * rad * rad; 22: Console.WriteLine(" The area for radius ({0}) is {1}", rad, theArea); 23: return theArea; 24: } 25: 26: public double Area(int x1, int y1, double rad) 27: { 28: return Area(rad); 29: } 30: 31: public double Area( int x1, int y1, int x2, int y2 ) 32: { 33: int x_diff; 34: int y_diff; 35: double rad; 36: 37: x_diff = x2 - x1; 38: y_diff = y2 - y1; 39: 40: rad = (double) Math.Sqrt((x_diff * x_diff) + (y_diff * y_diff)); 41: 42: return Area(rad); 43: } 44: 45: public Circle() 46: { 47: x = 0; 48: y = 0; 49: radius = 0.0; 50: } 51: } 52: 53: class CircleApp 54: { 55: public static void Main() 56: { 57: Circle myCircle = new Circle(); 58: 59: Console.WriteLine("Passing nothing..."); 60: myCircle.Area(); 61: 62: Console.WriteLine("\nPassing a radius of 3..."); 63: myCircle.Area( 3 ); 64: 65: Console.WriteLine("\nPassing a center of (2, 4) and a radius of 3..."); 66: myCircle.Area( 2, 4, 3 ); 67: 68: Console.WriteLine("\nPassing center of (2, 3) and a point of (5, 6)..."); 69: myCircle.Area( 2, 3,

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        Remove the line numbers with following colon at the beginning of each line.


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        L 1 Reply Last reply
        0
        • C Colin Angus Mackay

          I can't see it. You are going to have to provide the line numbers that the compiler is giving.


          Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

          L Offline
          L Offline
          Lurker00
          wrote on last edited by
          #4

          Is there an easy way to do that? (It comes out as a big mess if I copy/paste.) The first error is Line 1, Col 2 for some weird reason. I've actually gotten 18 errors.

          1 Reply Last reply
          0
          • S Stefan Troschuetz

            Remove the line numbers with following colon at the beginning of each line.


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            L Offline
            L Offline
            Lurker00
            wrote on last edited by
            #5

            Thanks Stefan! :) (Just noticed all the examples on cd for Day 8 of Sam's C# in 21 Days have the same problem.) On another note, is there a quick and easy way to remove a column of characters, for future reference? (And to fix the rest of the programs quickly.)

            R 1 Reply Last reply
            0
            • L Lurker00

              Thanks Stefan! :) (Just noticed all the examples on cd for Day 8 of Sam's C# in 21 Days have the same problem.) On another note, is there a quick and easy way to remove a column of characters, for future reference? (And to fix the rest of the programs quickly.)

              R Offline
              R Offline
              Russell Jones
              wrote on last edited by
              #6

              it might be worth trying sams website, there may be updated working versions of the code available for d/l Russ

              1 Reply Last reply
              0
              • L Lurker00

                Trying to run the following console program from a 'Sam's' Learn C# book and getting a bunch of errors; can anyone see what might be causing these please? (I'm only a C# rookie myself.) Error 1 A namespace does not directly contain members such as fields or methods Error 3 Invalid token '7' in class, struct, or interface member declaration Error 4 Invalid token '9' in class, struct, or interface member declaration Error 5 Invalid token '10' in class, struct, or interface member declaration Error 6 Invalid token '11' in class, struct, or interface member declaration Error 7 Invalid token '12' in class, struct, or interface member declaration 1: // Circle1.cs - Overloading the area method 2: //-------------------------------------------------------------------- 3: 4: using System; 5: 6: public class Circle 7: { 8: public int x; 9: public int y; 10: public double radius; 11: private const float PI = 3.14159F; 12: 13: public double Area() // Uses values from data members 14: { 15: return Area(radius); 16: } 17: 18: public double Area( double rad ) 19: { 20: double theArea; 21: theArea = PI * rad * rad; 22: Console.WriteLine(" The area for radius ({0}) is {1}", rad, theArea); 23: return theArea; 24: } 25: 26: public double Area(int x1, int y1, double rad) 27: { 28: return Area(rad); 29: } 30: 31: public double Area( int x1, int y1, int x2, int y2 ) 32: { 33: int x_diff; 34: int y_diff; 35: double rad; 36: 37: x_diff = x2 - x1; 38: y_diff = y2 - y1; 39: 40: rad = (double) Math.Sqrt((x_diff * x_diff) + (y_diff * y_diff)); 41: 42: return Area(rad); 43: } 44: 45: public Circle() 46: { 47: x = 0; 48: y = 0; 49: radius = 0.0; 50: } 51: } 52: 53: class CircleApp 54: { 55: public static void Main() 56: { 57: Circle myCircle = new Circle(); 58: 59: Console.WriteLine("Passing nothing..."); 60: myCircle.Area(); 61: 62: Console.WriteLine("\nPassing a radius of 3..."); 63: myCircle.Area( 3 ); 64: 65: Console.WriteLine("\nPassing a center of (2, 4) and a radius of 3..."); 66: myCircle.Area( 2, 4, 3 ); 67: 68: Console.WriteLine("\nPassing center of (2, 3) and a point of (5, 6)..."); 69: myCircle.Area( 2, 3,

                B Offline
                B Offline
                bobsugar222
                wrote on last edited by
                #7

                I copy and pasted the class you posted into Excel and did a text to columns with : separator. I then copy and pasted column B into notepad then from notepad to a New Console application in VS. It did error with "A namespace does not directly contain members such as fields or methods" on compilation. I looked through the code and noticed Excel had inserted a '0' above the line "class CircleApp". I removed this and it works perfectly.

                L 1 Reply Last reply
                0
                • B bobsugar222

                  I copy and pasted the class you posted into Excel and did a text to columns with : separator. I then copy and pasted column B into notepad then from notepad to a New Console application in VS. It did error with "A namespace does not directly contain members such as fields or methods" on compilation. I looked through the code and noticed Excel had inserted a '0' above the line "class CircleApp". I removed this and it works perfectly.

                  L Offline
                  L Offline
                  Lurker00
                  wrote on last edited by
                  #8

                  Not sure if fiddling about with text to columns would be quicker than a manual delete - I'm thinking the most efficient way to fix this and similar problems might be with an Excel VBA macro?

                  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