473,765 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.c heckedListBox1' denotes a 'field'
where a 'class' was expected
public static void MenuItem_Click( object sender, System.EventArg s e)
{
for(int i = 0; i<checkedListBo x1.Items.Count; i++)//<<<<line 1130
{
checkedListBox1 .SetItemChecked (i,false);
}
}

Also getting an error saying: C:\...\newPromp t.cs(172): An object reference
is required for the nonstatic field, method, or property
'APP.mainForm.M enuItem'
private void button1_Click(o bject sender, System.EventArg s e)
{
for(int o = mainForm.histor yMenuItem.MenuI tems.Count-1; o>=0;o--)
mainForm.histor yMenuItem.MenuI tems.RemoveAt(o );

}

Help is appreciated.

Nov 15 '05 #1
10 2211
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**********@o nlyonnewsgroup. com> wrote in message
news:#N******** ******@tk2msftn gp13.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.c heckedListBox1' denotes a 'field'
where a 'class' was expected
public static void MenuItem_Click( object sender, System.EventArg s e)
{
for(int i = 0; i<checkedListBo x1.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 *checkedListBox 1*.
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 *checkedListBox 1* 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:\...\newPromp t.cs(172): An object reference is required for the nonstatic field, method, or property
'APP.mainForm.M enuItem'
private void button1_Click(o bject sender, System.EventArg s e)
{
for(int o = mainForm.histor yMenuItem.MenuI tems.Count-1; o>=0;o--)
mainForm.histor yMenuItem.MenuI tems.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.
checkedListBox 1 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.c s(1130): 'APP.mainForm.c heckedListBox1'

denotes a 'field'
where a 'class' was expected
public static void MenuItem_Click( object sender,

System.EventArg s e)
{
for(int i = 0;

i<checkedListBo x1.Items.Count; i++)//<<<<line 1130
{
checkedListBox 1.SetItemChecke d(i,false);
}
}

Also getting an error saying: C:\...\newPromp t.cs(172):

An object reference
is required for the nonstatic field, method, or property
'APP.mainForm. MenuItem'
private void button1_Click(o bject sender,

System.EventArg s e)
{
for(int o = mainForm.histor yMenuItem.MenuI tems.Count-1;

o>=0;o--)
mainForm.histor yMenuItem.MenuI tems.RemoveAt(o );

}

Help is appreciated.

.

Nov 15 '05 #5

"100" <10*@100.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Hi Chris,
See comments inline.
C:\...\Form1.cs (1130): 'APP.mainForm.c heckedListBox1' denotes a 'field'
where a 'class' was expected
public static void MenuItem_Click( object sender, System.EventArg s e)
{
for(int i = 0; i<checkedListBo x1.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 *checkedListBox 1*.
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 *checkedListBox 1* 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:\...\newPromp t.cs(172): An object

reference
is required for the nonstatic field, method, or property
'APP.mainForm.M enuItem'
private void button1_Click(o bject sender, System.EventArg s e)
{
for(int o = mainForm.histor yMenuItem.MenuI tems.Count-1; o>=0;o--)
mainForm.histor yMenuItem.MenuI tems.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*****@hotmai l.com> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .
Hi

Make both the method and the field non-static?

Gary
"Chris Clement" <ch**********@o nlyonnewsgroup. com> wrote in message
news:#N******** ******@tk2msftn gp13.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 mystaticfunctio n(CheckedListBo x clb)
{
for(int i = 0; i<clb.Items.Cou nt;i++)
{
clb.SetItemChec ked(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.
>checkedListBox 1 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.c s(1130): 'APP.mainForm.c heckedListBox1'

denotes a 'field'
>where a 'class' was expected
>
>
>public static void MenuItem_Click( object sender,

System.EventArg s e)
>{
>for(int i = 0;

i<checkedListBo x1.Items.Count; i++)//<<<<line 1130
>{
>checkedListBox 1.SetItemChecke d(i,false);
>}
>}
>
>Also getting an error saying: C:\...\newPromp t.cs
(172): An object reference
>is required for the nonstatic field, method, or
property >'APP.mainForm. MenuItem'
>
>
>private void button1_Click(o bject sender,

System.EventArg s e)
>{
>for(int o = mainForm.histor yMenuItem.MenuI tems.Count-

1; o>=0;o--)
> mainForm.histor yMenuItem.MenuI tems.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 mystaticfunctio n(CheckedListBo x clb)
{
for(int i = 0; i<clb.Items.Cou nt;i++)
{
clb.SetItemChec ked(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.
>checkedListBox 1 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.c s(1130): 'APP.mainForm.c heckedListBox1'
denotes a 'field'
>where a 'class' was expected
>
>
>public static void MenuItem_Click( object sender,
System.EventArg s e)
>{
>for(int i = 0;
i<checkedListBo x1.Items.Count; i++)//<<<<line 1130
>{
>checkedListBox 1.SetItemChecke d(i,false);
>}
>}
>
>Also getting an error saying: C:\...\newPromp t.cs (172): An object reference
>is required for the nonstatic field, method, or property >'APP.mainForm. MenuItem'
>
>
>private void button1_Click(o bject sender,
System.EventArg s e)
>{
>for(int o = mainForm.histor yMenuItem.MenuI tems.Count- 1; o>=0;o--)
> mainForm.histor yMenuItem.MenuI tems.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

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

Similar topics

5
15841
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. He's wrong. Right? Two users in a web session could both access a static method at the same time and never have any trouble.
9
1980
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 interfere with one another. My question is, for each static method, do I need to lock access to only one call at a time? I've noticed that Microsofts Data Application block also uses static methods for its data access calls - for example execute...
3
2088
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 copy of data as opposed to multiple instances of the class) C# is now applying the static concept to methods. Why?, I thought that only one copy of the methods were loaded into memory not matter how many instances were created. Is this different...
9
4161
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 verbaige completely wrong, but here goes nothing ... I'm currently using the MS Data Access Block for a desktop application I'm writing. Recently, I had to add a call to a web service, which in
17
2354
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 oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
5
1960
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 multiple web application threads from using the same method at the same time? For example, I have a static method that extracts information from a page request cookie for use by other parts of the application. Since this happens on every request I...
4
2050
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 changing because they are not thread-safe)." Can someone please elaborate on the pros and cons of using static methods? (In VB they are called Shared methods.) The MS DAAB uses Shared methods. This means that all Data Access calls go through...
6
1459
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 interfere with one another. My question is, for each static method, do I need to lock access to only one call at a time? I've noticed that Microsofts Data Application block also uses static methods for its data access calls - for example execute...
6
1775
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 .... CallToStaticMethod();
1
3095
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 code where the problem is occurring. public class Employee { static ArrayList<Employee> arlist; int empid = arlist.size(); /** * displayMatch inputs a keyword from the user. It then iterates through the
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.