Recent

Author Topic: Default values in property?  (Read 648 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Default values in property?
« on: March 29, 2023, 05:08:42 am »
Is there any way that I can assign default values to property? Method's default values are not applied automatically. 


      function GetMessage(RState: integer; lang: string=''; GoNext: boolean=false;
        ivExists: boolean=false; pid: integer=0; wave: integer=0): string;

      property AQMessage[RState:integer; lang:string; gonext, ivexists:boolean;
               pid, wave: integer]: string read getMessage;                 

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Default values in property?
« Reply #1 on: March 29, 2023, 09:12:22 am »
Assign it a value in the constructor?

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Default values in property?
« Reply #2 on: March 29, 2023, 10:04:15 am »
Actually problem is I can't use in following way.

    s := AQMessage(100);
   

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Default values in property?
« Reply #3 on: March 29, 2023, 10:27:44 am »
Show your problem with more than 1 line of code would help us to help you.
How about to upload a small demo project that cover your problem?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Default values in property?
« Reply #4 on: March 29, 2023, 11:03:13 am »
So, you have a function, GetMessage, that might not give a "proper" result, in which case you want to return some default value?

Code: Pascal  [Select][+][-]
  1. function GetMessage: string;
  2. begin
  3.   Result := DEFAULT_VALUE;
  4.  
  5.   try
  6.     Result := GetTheMessage;
  7.   finally
  8.   end;
  9. end;

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Default values in property?
« Reply #5 on: March 29, 2023, 11:10:21 am »
I'll rewrite the initial example.

Code: Pascal  [Select][+][-]
  1. type
  2.     TMyObj = class
  3.     public
  4.           function GetMessage(RState: integer; lang: string=''; GoNext: boolean=false;
  5.                     ivExists: boolean=false; pid: integer=0; wave: integer=0): string;
  6.           property AQMessage[RState:integer; lang:string; gonext, ivexists:boolean;
  7.                     pid, wave: integer]: string read getMessage;                
  8.      end;
  9. var
  10.      MyObj: TMyObj;
  11.  

With the above example, I can write:       s := MyObj.GetMessage(100);
but not :                                             s :=  AQMessage(100);

because I cannot define like:

Code: Pascal  [Select][+][-]
  1.           property AQMessage[RState:integer; lang:string=''; gonext:boolean=false; ivexists:boolean=false;
  2.                     pid:integer=0; wave: integer=0]: string read getMessage;                
  3.  

And I'm asking whether there are any way that I can write:     s :=  MyObj.AQMessage(100);

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Default values in property?
« Reply #6 on: March 29, 2023, 11:31:11 am »
I'll rewrite the initial example.
(...)
And I'm asking whether there are any way that I can write:     s :=  MyObj.AQMessage(100);
So, AQMessage is more like a class function than a property/field, isn't it?

Then why not just use it as a class function and delete AQMessage (or rename GetMessage() to AQMessage())?
You don't have a single field for AQMessage (i.e. FAQMessage or something) so it shouldn't be a property.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Default values in property?
« Reply #7 on: March 29, 2023, 11:49:46 am »
Make your arguments as properties and use AQMessage without any arguments, inside GetMessage use fields from the properties (overload that method to either being a getter or a standalone method)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018