473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with static methods

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.

Nov 15 '05 #1
10 2179
Ok. I made the checkListBox1 static and the error went away, but now it
disappears from the form in design mode. Any ideas?
Nov 15 '05 #2
Hi

Make both the method and the field non-static?

Gary
"Chris Clement" <ch**********@onlyonnewsgroup.com> wrote in message
news:#N**************@tk2msftngp13.phx.gbl...
Ok. I made the checkListBox1 static and the error went away, but now it
disappears from the form in design mode. Any ideas?

Nov 15 '05 #3
100
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
Nov 15 '05 #4
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.
"Nelson" <ne*******@cox.net> wrote in message
news:00****************************@phx.gbl...

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
{
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.

.

Nov 15 '05 #5

"100" <10*@100.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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?

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.
Nov 15 '05 #6
Gary,

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

Chris
"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi

Make both the method and the field non-static?

Gary
"Chris Clement" <ch**********@onlyonnewsgroup.com> wrote in message
news:#N**************@tk2msftngp13.phx.gbl...
Ok. I made the checkListBox1 static and the error went away, but now it
disappears from the form in design mode. Any ideas?


Nov 15 '05 #7
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.
-----Original Message-----
The method is being called by another class so it needs to be static forthat class to access it with instantiation. Yes, what you are saying makessense and I actually fixed the error by making the checkListBox1 static, butnow the checkListBox no longer appears in design mode.
"Nelson" <ne*******@cox.net> wrote in message
news:00****************************@phx.gbl...

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
>{
>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.
>
>
>
>.
>

.

Nov 15 '05 #8
Thanks, Nelson. I'll give it a try.

"nelson" <ne*******@cox.net> wrote in message
news:00****************************@phx.gbl...
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.
-----Original Message-----
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.
"Nelson" <ne*******@cox.net> wrote in message
news:00****************************@phx.gbl...

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
>{
>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.
>
>
>
>.
>

.

Nov 15 '05 #9
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)
Nov 15 '05 #10

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 isexpecting 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 referenceis 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.

.

Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: GIMME | last post by:
One of my coworkers insists that one should never use static methods because race conditions exist. Thinking along the lines that all variable assignments are assignments to static variables. ...
9
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
5
by: blah, blah, blah | last post by:
I'm developing a .Net web application and created many helper classes often using static (shared in VB.Net) methods. Do I need to use the lock (SyncLock) statement in these methods to prevent...
4
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when...
6
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
1
by: sandyw | last post by:
I having trouble with my project and need a little help please "The method inputEmployee cannot be declared static; static methods can only be declared in a static or top level type" Part of my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.