Reputation: 37
I'm working on program in c# which uses a listbox as a selection method in its main form. It has functions where you can edit the items in the listbox.
I wanted to edit the items from a seperate dedicated form, so I made a new instance of the form but whenever I try to access the original form's functions (which I have made public) I get this error: Error 2 An object reference is required for the non-static field, method, or property
I've looked around on the internet quite a bit but all I see is people talking about using a static property on my function. However, when I do this I get more of the above errors popping up over variables and the like within the function
Here's the function in Form1 (which i'm trying to reference)
public void ReadConfig(string configFile)
{
fileList.Clear();
listBoxName.Items.Clear();
FileStream file = null;
if (!File.Exists(file))
{
MessageBox.Show(file + " was not found: Creating blank file");
using (file = File.Create(file)) ;
}
else
{
string line;
int lineNumber = 1;
// I cut out some long code here where the program reads from a file and saves it to an object
}
}
Here's a code snippet for where the error takes place (I cut some code where I save it to a text file, but the main part that's of concern is the Form1.ReadFile(Form1.file)
private void buttonSave_Click(object sender, EventArgs e)
{
string[] temp = File.ReadAllLines(Form1.file);
string[] newFile;
if (itemNew == true)
{
newFile = new string[temp.Length + 1];
}
else
{
newFile = new string[temp.Length];
}
for (int i = 0; i < temp.Length; i++)
{
newFile[i] = temp[i];
}
File.WriteAllLines(Form1.file, newFile);
ConfigForm.ReadFile(Form1.file);
this.Close();
}
I hope that's enough code to go off. My program is pretty long so I tried to keep it as short and direct as possible. Thanks for being patient with me =]
I'm pretty new at programming so if any kind souls happen to help could you keep it as simple as possible?
Thanks much =]
Upvotes: 0
Views: 211
Reputation: 2771
Here is a basic idea of what you are trying to achieve. A more advanced way of doing it would be with delegates / event handlers but wanted to keep it simple for now.
Form 1
public Form1()
{
InitializeComponent();
}
List<string> _items = new List<string>();
public void LoadListBoxWithItems()
{
for (int i = 0; i < 5; i++)
{
_items.Add(string.Format("My New Item {0}", i));
}
lbItems.DataSource = _items;
lbItems.Refresh();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.LoadValues(lbItems.SelectedItem.ToString(), this);
form2.ShowDialog();
}
public string GetCurrentItem()
{
return lbItems.SelectedItem.ToString();
}
public void UpdateItem(string item, string newitem)
{
int index = _items.IndexOf(item);
_items[index] = newitem;
lbItems.Refresh();
}
Form 2
public Form2()
{
InitializeComponent();
}
private string _originalvalue = null;
private Form1 _form1;
public void LoadValues(string item, Form1 form)
{
_originalvalue = item;
_form1 = form;
}
private void btnSave_Click(object sender, EventArgs e)
{
// Do work to change value
string newvalue = _originalvalue;
_form1.UpdateItem(newvalue, _originalvalue);
}
Upvotes: 0
Reputation: 5723
It's probably because you're trying to use functions that exist in your first window from the second one as if they were static, but they aren't.
You can try to solve this problem this way:
in the second form create a property with class of your first form, like:
class Form1 : Form
{
//this property will store reference to the first form
public Form1 AssociatedFirstForm { get; set; }
//...
}
Then in the code where you create the second form, assign the first form to this property, which can look like this (if you create the second form from the first one):
...
Form2 secondForm = new Form2();
secondForm.AssociatedFirstForm = this;
...
or like this (if you create both forms from another part of code):
...
Form1 firstForm = new Form1();
Form2 secondForm = new Form2();
secondForm.AssociatedFirstForm = firstForm;
...
then, in your second form you should be able to call a method from the first form like this:
...
var myResultFromAnotherWindow = this.AssociatedFirstForm.SampleMethodToCall();
...
I suppose you should also read more about using static classes and class members and creating instances of objects. It should make it clear to you how and when to use them.
Update
I haven't written it clear enough, but you should avoid to set expose methods or properties as public if it's not really needed.
If you want to create good code structure in your application and learn how it should be done, look for some articles about object oriented programming.
Some sample links:
Knowing eventing mechanism also can be useful: http://msdn.microsoft.com/en-us/library/awbftdfh.aspx.
Upvotes: 1
Reputation: 11088
You are calling method on type not on instance of a type. When methods are not declared as static you need to first instantiate the object that contains those methods (using new).
Upvotes: 0