Pass form object value to static method
I need to take a form object value and pass it into开发者_运维问答 a static method:
public void SetCalendarStartSafe(DateTime startDateSafe)
{
startDateSafe = calendarStart.Value;
}
private static DataTable GetData()
{
frmMain frm = new frmMain();
DateTime startDate = new frmMain();
frm.SetCalendarStartSafe(startDate);
}
However I keep getting today's current date whenever I try this approach, even if the specified calendar date on the form is different. How can I can the user-specified calendar date from the original frmMain object? Thanks in advance for any guidance.
you will be calling GetData() from somewhere in the code which is non-static? like for example from some event in the Form.. in that event pass the parameter to the static method GetData(DateTime..)
public void SetCalendarStartSafe(DateTime startDateSafe)
{
// wrong:
// startDateSafe = calendarStart.Value;
// right:
calendarStart.Value = startDateSafe;
}
There, just swap those two around. The destination of the assignment is on the left.
精彩评论