views:

155

answers:

4

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.

A: 

You 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.

Andrew Bezzub
@Andrew Bezzub, this method is called frequently and I want to make the performance of the application better so I have declared the method as static.
Harikrishna
@Harikrishna, I think it is not the right place for performance optimization.
Andrew Bezzub
@Andrew Bezzub, There may be almost ten combobox and even there is no fix number of combobox which can be on the window form.
Harikrishna
@Andrew Bezzub,Adrian Faciu says that use the keyword this and you will be able to do it but I don't understand how ?
Harikrishna
@Harikrishna: You only have one form. Therefore, there is no performance improvement is declaring the method static.
AMissico
@Harikrishna, I believe Andrew's point is that if you're unable to figure out how to use instance fields in a static method, perhaps performance optimization shouldn't be #1 on your priority list. Insert something here about premature optimization being the root of all evil.
Anthony Pegram
A: 

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.

AMissico
A: 

You can access the combobox by passing "this" to the static method and accessing any member that you need over "this".

Adrian Faciu
@Adrian Faciu, Please explain briefly..
Harikrishna
@Harikrishna, look at my answer. Except instead of receiving a TextBox like in my example, you would receive an object of the type of your form (Form1?). You could then call that function using "this" as the argument. The function itself could then access the accessible members of the parameter.
Anthony Pegram
@Harikrishna, Anthony is right, it would be the same but instead of a TextBox parameter you would have a Form. If you need only the data from the textBox you should go with that, with "this" you can access all the members of your form.
Adrian Faciu
A: 

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();
}
Anthony Pegram