Muhammed Naqi
Muhammed Naqi

Reputation: 47

How to change values of fields of a class(object) in form class

I design a time class

` 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;         
        }

`

I want to to create an object of this class in winform (Form class) and want to change values of class(object) fields on timer tick event.

I do like this.

 Time_Class t1 = new Time_Class();

        private void timer1_Tick(object sender, EventArgs e)
        {

            //code
        }

But in timer tick, object of this class is not calling, which i define out side of timer tick.

How i call an object of this class in timer tick and change the values of fields of that class(object).

Upvotes: 0

Views: 102

Answers (1)

apomene
apomene

Reputation: 14389

you have to make fields public:

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

Upvotes: 3

Related Questions