-1

I'm trying to create an object in my windows form app but if I create it in the constructor, then I can't access it in the entire app...(Like the events) In the code below the Time1 isn't available. I'll be happy to hear from you...

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...";
            }
        }
    }
}

The code below is my class:

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();
            }
        }
    }
}
Andy G
  • 19,232
  • 5
  • 47
  • 69
Amirhosein Al
  • 470
  • 6
  • 18
  • You have some serious design flows...why your ClockApp is derived from Forms class? instead write those methods as Getters and access theire return value from your form application. – Dumbo May 15 '16 at 17:45

1 Answers1

0
  • You dont need to create a new instance of ClockApp in the constructor.
    • Remove that line.
  • The field (or whatever) 'Time1' is not needed. Remove 'Time1.' completely form your code:
    • Time1.getHour = Convert.ToInt16(txtHour.Text); => getHour = Convert.ToInt16(txtHour.Text);
    • Time1.ToUniversal(); => ToUniversal();
    • and so on.

That should make your code at least compilable.


Bugs:

  • value > 23 && value < 0 is always false. You have to use || instead of &&.
  • same for value > 59 && value < 0

Code conventions:

I know you just starting with c#, but please check the common coding convention to improve the readabilty:

  • Properties: first letter is capitalized
  • Fiels: first letter is not capitalized
  • Do not start you propery name with 'get'. Just Hour, Min or Sec is perfect.
JanDotNet
  • 3,746
  • 21
  • 30