静态方法有问题 [英] Problem with static methods
问题描述
我没有写这段代码,所以我不确定这个错误是怎么回事。
checkedListBox1是一个公共对象。为什么错误表明在for循环中期望''类''是
?是因为该方法是静态的吗?
这是错误和代码:
C:\ ... \ Form1.cs(1130) ):''APP.mainForm.checkedListBox1''表示''字段''
,其中''类''是预期的
public static void MenuItem_Click(object sender ,System.EventArgs e)
{
for(int i = 0; i< checkedListBox1.Items.Count; i ++)//<<<< 1130行
{
checkedListBox1.SetItemChecked(i,false);
}
}
同样出错:C:\ ... \ newPrompt.cs(172):对象引用
是非静态字段方法,或者属性
''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);
}
帮助表示赞赏。
好的。我将checkListBox1设为静态,错误消失,但现在它在设计模式下从表单中消失了。任何想法?
您好
使方法和字段都是非静态的吗?
Gary
" Chris Clement" < CH ********** @ onlyonnewsgroup.com>在消息中写道
新闻:#N ************** @ tk2msftngp13.phx.gbl ...好的。我将checkListBox1设为静态,错误消失了,但现在它在设计模式下从表单中消失了。有什么想法吗?
你好Chris,
见内联评论。
C:\ ... \ Form1.cs(1130):''APP.mainForm.checkedListBox1''表示''字段''
其中''类''是预期的
public static void MenuItem_Click(object sender,System.EventArgs e)
{
for(int i = 0; i< checkedListBox1.Items.Count; i ++)//<< ;<< line 1130
{checkListBox1.SetItemChecked(i,false);
}
}
此方法是静态,所以你必须引用具有
这个公共字段的表单* checkedListBox1 *。
错误信息有点令人困惑,但因为它是静态方法你
只能使用(在方法参数和局部变量旁边)静态成员。
因为* checkedListBox1 *不是方法参数也不是局部变量它必须是
是某种类型的静态成员,它必须是预备的以类型名称结尾。
还得到一个错误说:C:\ ... \ newPrompt.cs(172):非静态字段需要一个对象
引用,方法或属性
''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我只能猜测这个错误。它是mainForm类的名称吗?
HTH
B \ rgds
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?
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?
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
这篇关于静态方法有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!