dronex dronex - 1 year ago 69
C# Question

Is it okay to just remove the "static" from a function?

I'm fairly new to programming,
so sorry if I mess some of the words up, also I guess this question is probably really stupid.

Anyways, I'm trying to control a C# browser window from a different thread.

The program has 2 windows. A console and the Form with the browser window.

namespace CodeSnippet
public partial class browserwindow : Form

public browserwindow()

//for the browser form to open, the console HAS to run in a seperate thread
Thread ConsoleThread = new Thread(new ThreadStart(TheConsole));


public static void TheConsole()
//read the input
string rawinput = Console.ReadLine();
string input = rawinput.ToLower();

//look for commands
if(input == "website")
string website = Console.ReadLine();

//TheBrowser is the name of the browser window


The "TheBrowser.Navigate" does not work in this piece of code.
However, if I remove the "static" on "TheConsole()" the code works perfectly fine.

Now my question is: Is it 'okay' to remove the static from the function?

Answer Source

static means you call it from the class, so browserwindow.Navigate(); would compile. Non-static means it has to be called from an instance of the class, so TheBrowserWindow.Navigate(); compiles successfully when the method is not static. That means you are telling that particular instance to invoke its Navigate method.

A non-static method has special access to the particular instance that called it, so it can say this.x to access the variable x of that instance, or this to reference instance itself.

You have not posted what the method Navigate does, but it sounds like it is appropriate for it to be non-static because it sounds like you are telling that particular instance of the browser object to navigate to a page. So it is probably better that you made it non-static. But if you post the code from that method you could get a better answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download