I have one combobox on the window form and I have one method which is declared with static like private static DataTable ParseTable(HtmlNode table)
Now I want to use combobox in that method for using combobox property but I can not access any property of combobox or combobox itself.If I made the combobox declaration as static then it can be accessed in that static method.But any alternative way to access combbox property in that static method because I don't want to make combobox declaration as static.
views:
155answers:
4You can pass combobox as a parameter to your method. Why do you need to have ParseTable
method as static?
Update: You cannot access non-static members of a class in static context. So the only thing you can do if you still need having a static method is somehow passing your combobox to that method using method's parameters.
There is no need for the static ParseTable method in the Form. Remove static from this function if you want that function to interact with controls on the form.
From reading the comments, there is no performance improvement if you only have one form. If you have multiple forms calling this static method then ParseTable should be moved into a separate static class.
If you are loading ten or more combo boxes using this ParseTable method, then I suggest you use Anthony Pegram and Andrew Bezzub suggestion and pass the ComboBox control as needed. I would avoid passing this (the form) because it generally creates "ugly" unmanageable code.
You can access the combobox by passing "this" to the static method and accessing any member that you need over "this".
You will not simply be able to access an instance member from a static function. One way to get access is you can pass the control into the function as an argument. Consider this example.
private void button1_Click(object sender, EventArgs e)
{
Form1.DoSomething(textBox1);
}
public static void DoSomething(TextBox textbox)
{
textbox.Text = DateTime.Now.ToString();
}