Name your class everything other than Main
(e.g. MainForm
).
Declaring Main as Static Throws Errors [closed]
-
27-09-2022 - |
Question
On my last compile, I received the following build errors:
"An object reference is required for the non-static field, method, or property ..."
and then a list of all of the items contained inside of my Main()
.
Previously, it read static Main() {
, but I could not get the errors to go away until I changed this to public Main() {
.
I don't recall the last thing I did before this started occurring (this was late last night), but I do believe I was messing with the static void recalcTotals()
trying to reference field items on the main form - to which I still have not figured it out, but that is a seperate issue.
Mind you, this is my first C# program. Below is basically my code:
namespace Play_XXX
{
public partial class Main : Form
{
// Enable moveability
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
// Handling the window messages
protected override void WndProc(ref Message message) {
base.WndProc(ref message);
if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
message.Result = (IntPtr)HTCAPTION;
}
public Main() {
InitializeComponent();
// Handle all auto-formatting textboxes
txt1.Leave += new EventHandler(validateInput);
txt2.Leave += new EventHandler(validateInput);
txt3.Leave += new EventHandler(validateInput);
txt4.Leave += new EventHandler(validateInput);
txt5.Leave += new EventHandler(validateInput);
txt6.Leave += new EventHandler(validateInput);
txt7.Leave += new EventHandler(validateInput);
txt8.Leave += new EventHandler(validateInput);
txt9.Leave += new EventHandler(validateInput);
txt10.Leave += new EventHandler(validateInput);
txt11.Leave += new EventHandler(validateInput);
}
private void Main_Load(object sender, EventArgs e) {
//TODO: Reference function to clear all input forms
}
static decimal? trueAmount(string testValue) {
decimal preOut;
//TODO: RegEx to remove all except digits?
if (testValue != null && testValue != "")
testValue = testValue.Replace(",", "").Replace("$", "");
else
testValue = "0";
//Return value
if (decimal.TryParse(testValue, out preOut))
return preOut;
else
return null;
}
void validateInput(object sender, EventArgs e) {
TextBox subjBox = (sender as TextBox);
decimal? trueVal = trueAmount(subjBox.Text);
//Check if this is a number
if (trueVal.HasValue) {
subjBox.Text = trueVal.Value.ToString("C");
subjBox.BackColor = Color.FromArgb(86, 86, 86);
subjBox.ForeColor = Color.FromArgb(208, 210, 211);
recalcTotals();
}
else {
subjBox.BackColor = Color.FromArgb(255, 200, 200);
subjBox.ForeColor = Color.Maroon;
}
}
static void recalcTotals() {
//TODO: How the fxck do your reference form controls
}
private void btnClose_Click(object sender, EventArgs e) {
Close();
}
}
}
La solution
Autres conseils
Note that a constructor looks different than a method.
A method has a name that is different from the class name, and it has a return type such as void
or int
. These are methods:
class C
{
public void M1()
{
}
public static void M2()
{
}
public int M3()
{
return 10;
}
public static int M4()
{
return -10;
}
}
Constructors must have the same name as the class and must not have a return type. These are constructors:
class C
{
public C()
{
}
static C()
{
}
}
Constructors are typically used to set up "state" before the class is used.
An entry point is a special method of which there can be only one in a program. The entry point must be a method, it must be static
and it must be named Main
. It follows that the entry point cannot be a direct member of a class that is also called Main
.
I don't think it is usual to have the entry point inside the class that derives from Form
, in a Windows Form application, but it should certainly be possible when the naming respects the above.
Furthermore, a non-static constructor or method can access all members of the class directly. When another non-static member is referred like that, the same instance of the class as the method/constructor belongs to (called this
) is implicitly used to access the other member.
In contrast, a static
constructor or method can only access a non-static member by explicitly having an instance, as in myInstance.TheNonStaticMember();
. Typically, the entry point, which is a static
method as I said, will create an instance with new TheClassName(...)
.