2016-05-15 55 views
-1

我想在我的Windows窗體應用程序來創建一個對象,但如果我在構造函數創建它,然後我不能訪問它在整個應用程序...(如事件)在時間1 ISN下面的代碼不可用。我會很高興收到你的來信......哪裏可以在windows窗體應用程序中創建我的對象?

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace ClockApp 
{ 
    public partial class ClockApp : Form 
    { 
     public ClockApp() 
     { 
      InitializeComponent(); 
      ClockApp Time1 = new ClockApp(); 
     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void ClockApp_Load(object sender, EventArgs e) 
     { 

     } 

     private void btnOk_Click(object sender, EventArgs e) 
     { 
      //ClockApp Time1 = new ClockApp(); 
      Time1.getHour = Convert.ToInt16(txtHour.Text); 
      Time1.getMin = Convert.ToInt16(txtMin.Text); 
      Time1.getSec = Convert.ToInt16(txtSec.Text); 
      if(rbUniversal.Checked == true) 
      { 
       Time1.ToUniversal(); 
      }else if(rbStandard.Checked == true) 
      { 
       Time1.ToStandard(); 
      } 
      else 
      { 
       lblTime.Text = "NOT Working..."; 
      } 
     } 
    } 
} 

下面的代碼是我的課:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace ClockApp 
{ 
    public partial class ClockApp : Form 
    { 
     // Fields 
     private int Hour; 
     private int Min; 
     private int Sec; 

     // Properties 
     public int getHour 
     { 
      get 
      { 
       return Hour; 
      } 
      set 
      { 
       if(value > 23 && value < 0) 
       { 
        Hour = 23; 
       } 
       else 
       { 
        Hour = value; 
       } 
      } 
     } 
     public int getMin 
     { 
      get 
      { 
       return Min; 
      } 
      set 
      { 
       if(value > 59 && value < 0) 
       { 
        Min = 59; 
       } 
       else 
       { 
        Min = value; 
       } 
      } 
     } 
     public int getSec 
     { 
      get 
      { 
       return Sec; 
      } 
      set 
      { 
       if(value > 59 && value < 0) 
       { 
        Sec = 59; 
       } 
       else 
       { 
        Sec = value; 
       } 
      } 
     } 

     // Constructors 


     // Methods 
     // ToUniversal() 
     public void ToUniversal() 
     { 
      lblTime.Text = Hour.ToString() + ":" + Min.ToString() + ":" + Sec.ToString(); 
     } 

     // ToStandard() 
     public void ToStandard() 
     { 
      if(Hour > 12) 
      { 
       int[] Modifier = new int[12]; 
       for (int i = 0; i < 12; i++) 
       { 
        Modifier[i] = i + 13; 
        if (Hour == Modifier[i]) 
        { 
         Hour = i+1; 
         lblAMPM.Text = "PM"; 
        } 

       } 
       lblTime.Text = Hour.ToString() + ":" + Min.ToString() + ":" + Sec.ToString(); 
      } 
      else 
      { 
       lblAMPM.Text = "AM"; 
       lblTime.Text = Hour.ToString() + ":" + Min.ToString() + ":" + Sec.ToString(); 
      } 
     } 
    } 
} 
+0

如果您在一個方法中定義並初始化一個對象,比如您的構造函數,那麼當該方法完成執行時它就會被銷燬。在c#中查找有關變量作用域的信息。 –

+0

你有一些嚴肅的設計流程...爲什麼你的ClockApp是從Forms類派生的?而是將這些方法編寫爲Getters並從窗體應用程序訪問它們的返回值。 –

回答

0
  • 你不需要在構造函數來創建的ClockApp一個新的實例。
    • 刪除了這一行。
  • 該字段(或其他)'Time1'是不需要的。刪除'Time1'。完全形成代碼:
    • Time1.getHour = Convert.ToInt16(txtHour.Text); =>getHour = Convert.ToInt16(txtHour.Text);
    • Time1.ToUniversal(); =>ToUniversal();
    • 等。

這應該使你的代碼至少編譯。


錯誤:

  • value > 23 && value < 0永遠是假的。你必須使用||而不是&&
  • 同爲value > 59 && value < 0

編碼規範:

我知道你剛開始使用C#,但請查看常見編碼約定,提高readabilty:

  • 屬性:第一個字母大寫
  • Fiels:第一個字母不是大寫
  • 不要以'get'開始你的名字。只是HourMinSec是完美的。
+1

它開始工作。我真的很感謝你的幫助JNS :-) –

+0

歡迎您:) – JanDotNet

相關問題