Errors with book example program
-
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,
-
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,
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
-
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,
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
-
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
-
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
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.)
-
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.)
it might be worth trying sams website, there may be updated working versions of the code available for d/l Russ
-
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,
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.
-
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.