Hello
I am writing a class Test. I considered a field “int age” to see how long each instance of this class has been created. In other words i want to know the age of each instantiated object of this class. I assumed i should use the Timer class. I wrote the following piece of code but it did not work. I am not sure if my approach is correct . I really appreciate any help.

Class Test
{

private System.Timers.Timer timerClock = new System.Timers.Timer();
        static int age= 0;
       
        public Test()
           {
           
               this.timerClock.Elapsed += new System.Timers.ElapsedEventHandler(process);
               this.timerClock.Interval = 1000;
               this.timerClock.Enabled = true;

        }
static void process(Object myObject, EventArgs myEventArgs)
        {
            
            age+= 1;

                   }

}

Hello
I am writing a class Test. I considered a field “int age” to see how long each instance of this class has been created. In other words i want to know the age of each instantiated object of this class. I assumed i should use the Timer class. I wrote the following piece of code but it did not work. I am not sure if my approach is correct . I really appreciate any help.

Class Test
{

private System.Timers.Timer timerClock = new System.Timers.Timer();
        static int age= 0;
       
        public Test()
           {
           
               this.timerClock.Elapsed += new System.Timers.ElapsedEventHandler(process);
               this.timerClock.Interval = 1000;
               this.timerClock.Enabled = true;

        }
static void process(Object myObject, EventArgs myEventArgs)
        {
            
            age+= 1;

                   }

}

You want to use DateTime and TimeSpan ...

Create a class variable called (say) _birth and in the constructor of the class set _birth to DateTime.Now

Then create a method called GetAge() and in there, use TimeSpan as you need to, and return whatever information is most appropriate.

If you need, I can bang out some code, but its all pretty straight forward if you just Google "C# DateTime" and "C# TimeSpan" on the web.

Edited 6 Years Ago by Zinderin: n/a

Hello
I am writing a class Test. I considered a field “int age” to see how long each instance of this class has been created. In other words i want to know the age of each instantiated object of this class. I assumed i should use the Timer class. I wrote the following piece of code but it did not work. I am not sure if my approach is correct . I really appreciate any help.

Class Test
{

private System.Timers.Timer timerClock = new System.Timers.Timer();
        static int age= 0;
       
        public Test()
           {
           
               this.timerClock.Elapsed += new System.Timers.ElapsedEventHandler(process);
               this.timerClock.Interval = 1000;
               this.timerClock.Enabled = true;

        }
static void process(Object myObject, EventArgs myEventArgs)
        {
            
            age+= 1;

                   }

}

I wrote an example ... just to make sure I wasn't smoking something ...

public class TestClass
{
    private DateTime myBirth;

    //The constructor...
    public TestClass()
    {
        myBirth = new DateTime();
    }

    // How many seconds old am I?
    public int MyAgeInSeconds()
    {
        TimeSpan myLife = DateTime.Now - this.myBirth;
        return myLife.Seconds;
    }
}

Please mark this thread as solved, if it takes care of your question.

Edited 6 Years Ago by Zinderin: n/a

Just a note, you probably want to use myLife.TotalSeconds; Seconds returns the seconds portion of the timespan where as TotalSeconds returns the whole of the TimeSpan converted to seconds. For example, if the TimeSpan is equal to 3 minutes and 25 seconds then:
Seconds == 25
TotalSeconds == (3 * 60) + 25 == 205

This question has already been answered. Start a new discussion instead.