2017-01-27 40 views
0

我设计一个时间类如何在窗体类改变类(对象)的字段的值

` class Time_Class 
    { 
     private int minute; 
     private int second; 
     private int hour; 

     //Default constructor 
     public Time_Class() 
     { 
      this.minute = 0; 
      this.second = 0; 
      this.hour = 0; 
     } 

     //Constructor overload 
     public Time_Class(int mm, int ss, int hh) 
     { 
      this.minute = mm; 
      this.second = ss; 
      this.hour = hh; 
     } 

     //Method for setting time 
     public void Set_Time(int mm,int ss,int hh) 
     { 
      this.minute = mm; 
      this.second = ss; 
      this.hour = hh;  
     } 

     //Method of Getting 24 Hour time 
     public string[] Get24Time() 
     { 
      int[] time = new int[3]; 
      time[0] = this.minute; 
      time[1] = this.second; 
      time[2] = this.hour; 

      if (time[1] > 59) 
      { 
       time[1] = 0; 
       time[0] += 1;   
      } 
      if (time[0] > 59) 
      { 
       time[0] = 0; 
       time[2] += 1; 
      } 
      if (time[2] > 24) 
      { 
       time[0] = 0; 
       time[1] = 0; 
       time[2] = 0; 
      } 

      string[] ret = new string[3]; 
      ret[0] = time[0].ToString(); 
      ret[1] = time[1].ToString(); 
      ret[2] = time[2].ToString(); 

      return ret;   
     } 

     //Method of Getting 12 Houur time 
     public string[] Get12Time() 
     { 
      string ampm = "AM"; 
      int[] time = new int[2]; 
      time[0] = this.minute; 
      time[1] = this.second; 
      time[2] = this.hour; 

      if (time[1] > 59) 
      { 
       time[1] = 0; 
       time[0] += 1; 
      } 
      if (time[0] > 59) 
      { 
       time[0] = 0; 
       time[2] += 1; 
      } 
      if (time[2] > 12) 
      { 
       time[0] = 0; 
       time[1] = 0; 
       time[2] = 0; 
       if (ampm == "PM") 
       { 
        ampm = "AM"; 
        goto b; 
       } 
       if (ampm=="AM") 
       { 
        ampm = "PM"; 
       } 

      } 
     b: 
      string[] ret = new string[3]; 
      ret[0] = time[0].ToString(); 
      ret[1] = time[1].ToString(); 
      ret[2] = time[2].ToString(); 
      ret[3] = ampm; 

      return ret;   
     } 

`

我想创造的winform这个类的一个对象(Form类)并且想要在计时器滴答事件中更改类(对象)字段的值。

我喜欢。

Time_Class t1 = new Time_Class(); 

     private void timer1_Tick(object sender, EventArgs e) 
     { 

      //code 
     } 

但是在计时器滴答声中,这个类的对象没有调用,我定义了计时器滴答声的一面。

我如何在计时器滴答中调用这个类的对象并更改该类(对象)的字段值。

+0

请抬头看'else'和'其他if'控制流语句,并删除'goto' – pinkfloydx33

+0

是的,它是Windows窗体计时器和enabled属性是假,但我开始计时器加载泡沫事件像'timer1.Start();' –

+0

@ pinkfloydx33是的,我会做到这一点,但没有关系的要求。 –

回答

3

你必须让公众领域:

public int minute {get;set;} 
public int second {get;set;} 
public int hour {get;set;} 
+0

是的。现在对象正在调用计时器打勾。 –

相关问题