Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7900369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:57:13+00:00 2026-06-03T08:57:13+00:00

I started learning Delphi two days ago but I got stuck. I broke down

  • 0

I started learning Delphi two days ago but I got stuck. I broke down because nothing goes my way so I decided to write here. I wanted to create class that would have a field with its own TTimer object and which will perform some action at some time interval. Is it even possible? Suppose we have such code:

Sth = class
private

public
  clock:TTimer;
  procedure clockTimer(Sender: TObject);
  constructor Create();
end;

constructor Sth.Create()
begin
  clock.interval:=1000;
  clock.OnTimer := clockTimer;
end;

procedure Sth.clockTimer(Sender: TObject);
begin
  //some action on this Sth object at clock.interval time...
end;

My similar code copiles but it doesn’t work properly. When I call the constructor the program crashes down (access violation at line: clock.interval:=1000;). I don’t know what

Sender:TObject 

does but I think that’s not the problem. Is it possible to create such class I want to?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:57:14+00:00Added an answer on June 3, 2026 at 8:57 am

    You have not created the timer. Declaring a variable is not enough. You do need to create the timer.

    constructor Sth.Create()
    begin
      clock := TTimer.Create(nil);
      clock.interval:=1000;
      clock.OnTimer := clockTimer;
    end;
    

    And you should destroy it too. Add a destructor to the class

    destructor Destroy; override;
    

    and implement it like this

    destructor Sth.Destroy;
    begin
      clock.Free;
      inherited;
    end;
    

    I would also recommend that you make your clock field have private visibility. It’s not good to expose the internals of a class like that.

    TMyClass = class
    private
      FClock: TTimer;
      procedure ClockTimer(Sender: TObject);
    public
      constructor Create;
      destructor Destroy; override;
    end;
    ....
    constructor TMyClass.Create
    begin
      inherited;
      FTimer := TTimer.Create(nil);
      FTimer.Interval := 1000;
      FTimer.OnTimer := ClockTimer;
    end;
    
    destructor TMyClass.Destroy;
    begin
      FTimer.Free;
      inherited;
    end;
    

    Note that I have included calls to the inherited constructor and destructor. These are not necessary in this class since it derives directly from TObject and the constructor and destructor for TObject is empty. But if you change the inheritance at some point, and make your class derive from a different class, then you will need to do this. So, in my view, it is good practise to include these calls always.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started learning the scala actors framework about two days ago. To make the
I started learning Rails only a few days ago, but in the mean time
I started learning haskell yesterday and I got stuck on a problem. After a
I started learning REGEX 2 days ago , now id like to make a
I started learning rails a few days ago and I'm reading head first rails
Started learning java a week ago and also decided learning the right way to
I started learning scala a few days ago and when learning it, I am
Just started learning Php on my own, and got stuck at the beginning. This
I have started learning powershell a couple of days ago, and I couldn't find
I started learning JavaScript a while ago. It's a fairly easy programming language considering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.