static - non static

Jedhi

Centurion
Joined
Oct 2, 2003
Messages
127
I have a ListView on Form1 that I update on different times. My problem is that when I am in function Test() I need to call the function UpdateListView. The only way I can this is to make UpdateListView static, but then I get the error "An object reference is required for the nonstatic field method". How do I solve this problem


Class Form1
{
X x = new X();

TestListView()
{
x.Test()
}

UpdateListView()
{
listView1.Items.Add
}
}


Class X()
{
Test()
{
How do I call UpdateListView
}
}
 
You can't make UpdateListView static because it references an instance member, listView1. Static methods can not access instance members. (How would the compiler know which instance you are referring to?) You need to pass Form1 to Test() so that Test can refer to an instance of Form1.

C#:
Class Form1
{
X x = new X();

TestListView()
{
x.Test(this)
}

UpdateListView()
{
listView1.Items.Add
}
}


Class X()
{
Test(Form1 form)
{
//How do I call UpdateListView

//Like this:
form.UpdateListView()
}
}
 
Back
Top