开发者

Pass form object value to static method

开发者 https://www.devze.com 2023-02-01 16:06 出处:网络
I need to take a form object value and pass it into开发者_运维问答 a static method: public void SetCalendarStartSafe(DateTime startDateSafe)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消