-1

i get this error when ever i try to compile my code here is the error

Error 1 An object reference is required for the non-static field, method, or property

below is my code

private void btnGenerate_Click(object sender, EventArgs e)
{
    frmFaultyDeviceByPeriod.FirstDate = dateTimePicker1.Value;
    frmFaultyDeviceByPeriod.SeconDate = dateTimePicker2.Value;
    frmFaultyDeviceByPeriod.Show();
}

the form am trying to call has the below code

public System.DateTime FirstDate;
public System.DateTime SecondDate;

what am i doing wrong.. help please

Sinatr
  • 20,892
  • 15
  • 90
  • 319
Umar E. Shaibu
  • 35
  • 2
  • 10

1 Answers1

2

Yes, well, FirstDate and SecondDate are instance members, so you need to set them using an object, not a class.

The error would be more evident if you adhered to the coding style guidelines, starting class names in capital letter.

The way to do it is this:

frmFaultyDeviceByPeriod frm = new frmFaultyDeviceByPeriod();
frm.FirstDate = dateTimePicker1.Value;
frm.SeconDate = dateTimePicker2.Value;
frm.Show();
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139