Im a new programmer and have a book (SAMS) learn C# in 24 hours.
in making a simple sample prograam, I keep getting the same error - in fact same error for all three chunk of code: error is as follows:
Error 1 Expected class, delegate, enum, interface, or struct
i get this error when trying to create a method - here is short bkgrnd - I am using ORCA version of C# so the book may be a little diff than actual program .. but the book keeps saying to set the Main() entry point to the name of the project - but the name of the project is already there - so im thinking that because of these small differences - there is something im overlookning to make the code work properly -
here is the code that prompts the error:
public
static void DrawEllipse(System.Windows.Forms.Form frn){
System.Drawing.Graphics objGraphics;
System.Drawing.Rectangle recDrawRectangle;
recDrawRectangle = frm.DisplayRectangle;
recDrawRectangle.Inflate(-5, -5);
objGraphics = frm.CreateGraphics();
objGraphics.Clear(System.Drawing.SystemColors.Control);
objGraphics.DrawEllipse(System.Drawing.Pens.Blue,recDrawRectangle);
objGraphics.Dispose();
}
thank you very much in advance,
J
JasonPosted Aug 2, 2007, 2:33 PM
Jan MontanoPosted Jul 31, 2007, 9:59 PM
Sam has a point with the System.Windows.Forms.Form frm.
The wrong placement of curly braces {} is the reason you're getting that error. After editing the curly braces, your code compiled well.
public class clsStaticExample
{
}
Just make sure that these codes must be inside the curly braces of your namespace. The misplaced curly braces made your methods other than the clsStaticExample() constructor outside the curly braces of your namespace. This is what caused Error 1 Expected class, delegate, enum, interface, or struct . It was not expecting to see a public static method definition.
JasonPosted Jul 31, 2007, 6:58 PM
thanks for you help ... basically i am trying to runa sample program that draws a circle when you click the butotn .. here is all the code:
using
System;using
System.Collections.Generic;using
System.Linq;using
System.Text;namespace
Static_Methods{
public class clsStaticExample{
public clsStaticExample(){
}
}
}
public
static void DrawEllipse(System.Windows.Forms.Form frn){
System.Drawing.Graphics objGraphics;
System.Drawing.Rectangle recDrawRectangle;
recDrawRectangle = frm.DisplayRectangle;
recDrawRectangle.Inflate(-5, -5);
objGraphics = frm.CreateGraphics();
objGraphics.Clear(System.Drawing.SystemColors.Control);
objGraphics.DrawEllipse(System.Drawing.Pens.Blue,recDrawRectangle);
objGraphics.Dispose();
}
public
static void ClearEllipse(System.Windows.Forms.Form.frm){
frm.Refresh();
}
public
static int ComputeLength(string strText){
return
strText.Length;}
i will try your suggestion about removing the . from the statement
SamPosted Jul 31, 2007, 6:08 PM
public static void ClearEllipse(System.Windows.Forms.Form frm)// not . between Form & frm
Not sure about second problem
can you explain in more detail
It may help if you can explain what you're
trying to do or if you can provide more source code.
Sam.
JasonPosted Jul 31, 2007, 5:46 PM
here are two other chuncks that both get the same error message:
public
static void ClearEllipse(System.Windows.Forms.Form.frm){
frm.Refresh();
}
public
static int ComputeLength(string strText){
return
strText.Length;}
in EVERY case , ithas the variable type underlined as an error - ex: "int" will be underlined or "void" willbe underlined.
here is the heading to the file:
using
System;using
System.Collections.Generic;using
System.Linq;using
System.Text;namespace
Static_Methods{
public class clsStaticExample{
public clsStaticExample(){
}
}
}
maybe something here is not up to specs - thanks
SamPosted Jul 31, 2007, 5:28 PM
You've named your parameter frn not frm!
Apart from that ithe code seems to work fine.
Hope this helps,
Sam.