Richard Mark Bonifacio Richard Mark Bonifacio - 1 year ago 84
C# Question

Error says(in infoGather textbox) "an object reference is required for non static field ..."

This is the first time I encounter this error in a textbox

private static void infoGather_keydown(object sender,KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (username == null)
{
username = infoGather.Text;
}
else
{
password = infoGather.Text;
if (username == "admin" && password == "admin")
{
MessageBox.Show("welcome");
}
}
}
}


This is the first time I encounter it in a
TextBox

Answer Source

Your TextBox infoGatheran is a member of an instance of the class, and is not static. Therefore you can't access it though a static function.

If you think about it it doesn't make sense - A static method belongs to the class and not a specific instance, therefore when you try to access a member of an instance - of which instance will it be?

To solve the specific error - is there any real reason to have this method static? If not just remove the static from the signature.

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