Problem with static methods

  • Thread starter Thread starter Chris Clement
  • Start date Start date
C

Chris Clement

I didn't write this code so I'm not sure what is going on with this error.
checkedListBox1 is a public object. Why does the error say that it is
expecting a 'class' in a for loop? Is it because the method is static?
Here is the error and the code:

C:\...\Form1.cs(1130): 'APP.mainForm.checkedListBox1' denotes a 'field'
where a 'class' was expected


public static void MenuItem_Click(object sender, System.EventArgs e)
{
for(int i = 0; i<checkedListBox1.Items.Count;i++)//<<<<line 1130
{
checkedListBox1.SetItemChecked(i,false);
}
}

Also getting an error saying: C:\...\newPrompt.cs(172): An object reference
is required for the nonstatic field, method, or property
'APP.mainForm.MenuItem'


private void button1_Click(object sender, System.EventArgs e)
{
for(int o = mainForm.historyMenuItem.MenuItems.Count-1; o>=0;o--)
mainForm.historyMenuItem.MenuItems.RemoveAt(o);

}

Help is appreciated.
 
Ok. I made the checkListBox1 static and the error went away, but now it
disappears from the form in design mode. Any ideas?
 
Since your function is declared static, it can be called
without instantiation of your form class wich contains
the checkbox. Therefore your static function has know
way of knowing a checkbox exists. Why are you using a
static function here anyway?

I hope that makes sense.
-----Original Message-----
I didn't write this code so I'm not sure what is going on with this error.
checkedListBox1 is a public object. Why does the error say that it is
expecting a 'class' in a for loop? Is it because the method is static?
Here is the error and the code:

C:\...\Form1.cs(1130): 'APP.mainForm.checkedListBox1' denotes a 'field'
where a 'class' was expected


public static void MenuItem_Click(object sender, System.EventArgs e)
{
for(int i = 0;
i<checkedListBox1.Items.Count;i++)//<<<<line 1130
 
The method is being called by another class so it needs to be static for
that class to access it with instantiation. Yes, what you are saying makes
sense and I actually fixed the error by making the checkListBox1 static, but
now the checkListBox no longer appears in design mode.
 
100 said:
Hi Chris,
See comments inline.


this method is static so you have to have reference to the form which has
this public field *checkedListBox1*.
The error message is somehow confusing, but since it is static method you
can use (beside method arguments and local variables) only static members.
Since *checkedListBox1* is not method argument nor local variable it has to
be a static member of some type ant it has to be preceded by a type name.


For this error I can only guess. Is it mainForm the name of the class?
Thanks for the help, 100. Yes, mainForm is the name of the class where the
static method is. Fixed the problem by making checkListBox1 static but now
it does not appear in the design mode.
 
Hi Chris,
See comments inline.
C:\...\Form1.cs(1130): 'APP.mainForm.checkedListBox1' denotes a 'field'
where a 'class' was expected


public static void MenuItem_Click(object sender, System.EventArgs e)
{
for(int i = 0; i<checkedListBox1.Items.Count;i++)//<<<<line 1130
{
checkedListBox1.SetItemChecked(i,false);
}
}

this method is static so you have to have reference to the form which has
this public field *checkedListBox1*.
The error message is somehow confusing, but since it is static method you
can use (beside method arguments and local variables) only static members.
Since *checkedListBox1* is not method argument nor local variable it has to
be a static member of some type ant it has to be preceded by a type name.
Also getting an error saying: C:\...\newPrompt.cs(172): An object reference
is required for the nonstatic field, method, or property
'APP.mainForm.MenuItem'


private void button1_Click(object sender, System.EventArgs e)
{
for(int o = mainForm.historyMenuItem.MenuItems.Count-1; o>=0;o--)
mainForm.historyMenuItem.MenuItems.RemoveAt(o);

}

For this error I can only guess. Is it mainForm the name of the class?

HTH
B\rgds
100
 
Gary,

Thanks for the response. The newPrompt class needs to be able to access the
method so it must be static.

Chris
 
Chris,

Why don't you just pass the checklistbox to the function.

Example:
public static void mystaticfunction(CheckedListBox clb)
{
for(int i = 0; i<clb.Items.Count;i++)
{
clb.SetItemChecked(i,false);
}
}

This should solve your problem.
 
Thanks, Nelson. I'll give it a try.

nelson said:
Chris,

Why don't you just pass the checklistbox to the function.

Example:
public static void mystaticfunction(CheckedListBox clb)
{
for(int i = 0; i<clb.Items.Count;i++)
{
clb.SetItemChecked(i,false);
}
}

This should solve your problem.
 
Chris,

I've read all the responses in this thread and remain unclear on the
need for a static method. Yes, you need to access this method from
another class but marking it public takes care of that problem.

It seems you're misundertanding the purpose of a static function. The
compiler expects to find the CheckedListBox within a containing type,
i.e. your form. However, since the method is static, there is no
instance of the form for it to access so it cannot locate the
CheckedListBox.

You either have to pass the object into the static method or make the
object a static member of your form. The second option makes little
sense since the only time you need something like a ListBox is as part
of a UI so that leaves you with the first option.

Personally, I'd recommend making the method non-static. Perhaps if you
gave us some more context, we could provide you with a better answer.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Back
Top