Recent

Author Topic: [SOLVED] Use Create (class) with parameters and overload  (Read 979 times)

Hansvb

  • Sr. Member
  • ****
  • Posts: 397
[SOLVED] Use Create (class) with parameters and overload
« on: August 02, 2021, 10:40:33 am »
hi,

This is not allowed ?
Code: Pascal  [Select][+][-]
  1. constructor Create(ConnectionName, Password, SchemaName, DatabaseName : String);
  2. begin
  3.   inherited;  --> Goes wrong:  oracleconnectionmaintain.pas(107,12) Error: Wrong number of parameters specified for call to "Create"
  4. end;

My base class is:

Code: Pascal  [Select][+][-]
  1. unit SQliteConnection;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Settings, SQLite3Conn, SQLDB;
  9.  
  10. type
  11.  
  12.   { TSQliteConnection }
  13.  
  14.   TSQliteConnection = class(TObject)
  15.  
  16.     private
  17.       FAppDatabaseFolder, FAppDatabaseFile : String;
  18.  
  19.       function GetAppDatabaseFile: String;
  20.       function GetAppDatabaseFolder: String;
  21.       procedure SetAppDatabaseFile(AValue: String);
  22.       procedure SetAppDatabaseFolder(AValue: String);
  23.  
  24.  
  25.     protected
  26.       SQLite3Conn: TSQLite3Connection;
  27.       SQLTransaction: TSQLTransaction;
  28.  
  29.     public
  30.       constructor Create; overload;
  31.       destructor Destroy; override;
  32.       property AppDatabaseFolder : String Read GetAppDatabaseFolder Write SetAppDatabaseFolder;
  33.       property AppDatabaseFile : String  Read GetAppDatabaseFile Write SetAppDatabaseFile;
  34.  
  35.   end;
  36.  
  37. implementation
  38.  
  39. { TSQliteConnection }
  40.  
  41. {%region Properties}
  42. ...
  43. {%endregion Properties}
  44.  
  45. constructor TSQliteConnection.Create;
  46. begin
  47.   inherited;
  48.   AppDatabaseFolder := ExtractFilePath(ParamStr(0));
  49.   AppDatabaseFile := AppDatabaseFolder + Settings.DatabaseFolder + PathDelim + Settings.DatabaseName;
  50.  
  51.   SQLite3Conn := TSQLite3Connection.Create(nil);
  52.   SQLTransaction := TSQLTransaction.Create(nil);
  53.  
  54.   SQLite3Conn.Transaction := SQLTransaction;
  55.   SQLTransaction.DataBase := SQLite3Conn;
  56.  
  57. end;
  58.  
  59. destructor TSQliteConnection.Destroy;
  60. begin
  61.   if assigned(SQLite3Conn) then SQLite3Conn.Free;
  62.   if assigned(SQLTransaction) then SQLTransaction.Free;
  63.  
  64.   inherited Destroy;
  65. end;
  66.  
  67. end.


The class where the create goes wrong has:


Code: Pascal  [Select][+][-]
  1. uses ..., SQliteConnection;
  2.  
  3. type
  4.   { TOraConmaintain }
  5.   TOraConmaintain = class(TSQliteConnection)
  6.   ...
  7. public
  8.   //constructor Create; overload;
  9.   constructor Create(ConnectionName, Password, SchemaName, DatabaseName : String);   overload;
  10.          
  11. ...
  12.  
  13. constructor TOraConmaintain.Create(ConnectionName, Password, SchemaName,
  14.   DatabaseName: String);
  15. begin
  16.   inherited;  --> Goes wrong:  oracleconnectionmaintain.pas(107,12) Error: Wrong number of parameters specified for call to "Create"
  17.  
  18.   OraConnectionName := ConnectionName;
  19.   OraSchemaName := SchemaName;
  20.   OraPassword := Password;
  21.   OraDatabaseName := DatabaseName;
  22. end;     

When i remove "inherited;" then the program compiles but goes wrong when i execute it. I think that is because the create in the base class is not executed.

Is it not possible to use overload on a create with parameters?
« Last Edit: August 02, 2021, 11:04:03 am by Hansvb »
OS Windows 10 - Lazarus 2.0.12

Bart

  • Hero Member
  • *****
  • Posts: 4433
    • Bart en Mariska's Webstek
Re: Use Create (class) with parameters and overload
« Reply #1 on: August 02, 2021, 10:50:34 am »
Does this work in the overloaded constructor?

Code: Pascal  [Select][+][-]
  1.   inherited Create;
  2.   ...

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Use Create (class) with parameters and overload
« Reply #2 on: August 02, 2021, 10:57:19 am »
When you use only "inherited" it will look for a method with the same signature than the current one, which in this case doesn't exists. Instead it finds the original Create and tries to see if it can be called with the parameters of the current one, which it can't; hence the error.

Specifying which inherited method you want, as Bart showed, circumvents this and calls the exact method you want.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hansvb

  • Sr. Member
  • ****
  • Posts: 397
Re: Use Create (class) with parameters and overload
« Reply #3 on: August 02, 2021, 11:03:46 am »
Quote
When you use only "inherited" it will look for a method with the same signature than the current one, which in this case doesn't exists. Instead it finds the original Create and tries to see if it can be called with the parameters of the current one, which it can't; hence the error.

I saw this just after posting my question. In the message window i saw an other error which pointed to the create function of my base class.
Good to know.

Adding
Code: Pascal  [Select][+][-]
  1. inherited Create;

This does the trick. No errors when compiling and no errors when executing.

Thanks!

OS Windows 10 - Lazarus 2.0.12

 

TinyPortal © 2005-2018