Delphi classes question

Please discuss general Delphi programming topics here.

Delphi classes question

Postby Klamber|ext. » September 27th, 2010, 11:37 am

hi
I have been messing with a lille code about 3 days now. And i don't get it...
I might be a bit noob in delphi but maybe someone can help me out..
Should be simple .. i just don't get it...

I want to make a class like that for ex.
Code: Select all
...

type
  THero = class(TObject)

    TCPCli : TIdTCPClient;

    constructor Create;
    procedure connect;
  private

....
implementation
..
constructor THero.create;
begin
          TCPCli := TidTCPClient.Create(nil);
end;

.....


ok .. now isaved it as MyClass.pas
and use it in my main code

Code: Select all
uses MyClass, ....


and a piece of code how i want to use it:
Code: Select all
...
var
  Form1: TForm1;
  superman :THero ;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
           superman.create;
end;



Thing is that it gives me an Access Violation error...

How could i make such a class??

Thanks
Klamber|ext.
Member
Member
 
Posts: 3
Joined: September 27th, 2010, 11:22 am

Re: Delphi classes question

Postby actalk » September 27th, 2010, 12:45 pm

Hello,

as I can see, you should try this form of creation.

Code: Select all
procedure TForm1.Button1Click(Sender: TObject);
begin
           //superman.create;
           superman := THero.Create;
end;
actalk
Junior Member
Junior Member
 
Posts: 29
Joined: October 5th, 2007, 3:56 pm

Re: Delphi classes question

Postby Klamber|ext. » September 27th, 2010, 1:08 pm

OMG!!!!!

That is so ovious. :)
I have been creating components on runtime before .. but just now i forgot it.

Thank you! saved my day...a week
Klamber|ext.
Member
Member
 
Posts: 3
Joined: September 27th, 2010, 11:22 am

Re: Delphi classes question

Postby Kambiz » September 27th, 2010, 4:47 pm

Don't forget to release TCPCli object on destructor of your class.

Code: Select all
THero = class(TObject)

  TCPCli : TIdTCPClient;

  constructor Create;
  destructor Destroy; override;

  .
  .
  .

end;

constructor THero.Create;
begin
  TCPCli := TIdTCPClient.Create(nil);
end;

destructor THero.Destroy;
begin
  TCPCli.Free;
  inherited Destroy;
end;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron