在Windows窗体应用程序中创建对象的位置?

时间:2016-05-15 17:21:33

标签: c# object encapsulation winapp

我试图在我的Windows窗体应用程序中创建一个对象,但如果我在构造函数中创建它,那么我就无法在整个应用程序中访问它...(就像事件一样) Time1以下的代码不可用。我很高兴收到你的来信......

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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  • 您不需要在构造函数中创建this.props.children.map(function(child, i) { ... }, this); ___^^^^_ 的新实例。
    • 删除该行。
  • 该字段(或其他)&#39; Time1&#39;不需要。删除&#39; Time1。&#39;完全形成你的代码:
    • ClockApp =&gt; Time1.getHour = Convert.ToInt16(txtHour.Text);
    • getHour = Convert.ToInt16(txtHour.Text); =&gt; Time1.ToUniversal();
    • 等等。

这应该使您的代码至少可以编译。

<强>错误:

  • ToUniversal();总是假的。您必须使用value > 23 && value < 0代替||
  • &&
  • 相同

代码约定:

我知道你刚开始使用c#,但请检查通用编码约定以提高可读性:

  • 属性:首字母大写
  • Fiels:第一封信没有大写
  • 不要以&#39; get&#39;开始你的名字。只需value > 59 && value < 0HourMin即可。