Recent

Author Topic: Can a class property be overloaded???  (Read 7406 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Can a class property be overloaded???
« on: March 24, 2011, 05:44:18 pm »
Not even sure I worded this correctly so I'll describe what I'd like to do.

I have a class that has a property of Quantity which is an integer, the class is designed to take the an inputted integer, convert it to a string and pad with spaces, so integer 9 becomes string '9     '.   It would be nice if I could 'overload' the property to allow an integer to be put in but a string read out so instead of having another property called QuantityString I could just read out Quantity.  Once put in the class the integer Quantity will never be read out as a number so I don't need to see it again.   Since I do this quite frequently, using a class to format output, I'd love to do it this way but I'm not sure if it's even possible.

Bill

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can a class property be overloaded???
« Reply #1 on: March 24, 2011, 06:24:29 pm »
I don't understand what exactly you try to reach.

Overloaded properties are not allowed, see: http://bugs.freepascal.org/view.php?id=12737

If you only need read something and get some result then overloaded function is not enough for you ?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Can a class property be overloaded???
« Reply #2 on: March 24, 2011, 06:48:00 pm »
I'd like to have done something like this:

var
A: integer;
B: string;

begin
A :=10;
Test.Qty := A;

B := Test.Qty;
end;

I thought it would have made the class much neater looking instead of having multiple properties for Qty.

Guess I can't so I'm back to separate properties for input and output.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Can a class property be overloaded???
« Reply #3 on: March 24, 2011, 06:57:29 pm »
The code might look neater but it's actually more confusing. That's why I never use Variants in my code (this could be your solution as well, but be aware with the speed and size penalty). An explicit conversion function or input / output separation like your previous approach is easier to understand.

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Can a class property be overloaded???
« Reply #4 on: March 24, 2011, 08:33:43 pm »
I changed how the class was functioning so right now I'm good.  I will need to look up what a 'variant' is since I'd like to try this in the future.

Thanks

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Can a class property be overloaded???
« Reply #5 on: March 25, 2011, 01:36:43 pm »
Quote
I will need to look up what a 'variant' is since I'd like to try this in the future.
Read the documentation.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Can a class property be overloaded???
« Reply #6 on: March 25, 2011, 04:53:40 pm »
You can do this:

Code: Pascal  [Select][+][-]
  1.   ...
  2.   procedure GetQuantity(out AQty: Integer);
  3.   procedure GetQuantity(out AQty: string);
  4.   ...
  5.  
  6. var
  7.   A: integer;
  8.   B: string;
  9. begin
  10.   A :=10;
  11.   Test.Qty := A;
  12.  
  13.   GetQuantity(B);
  14. end;
  15.  

or a different approach which is not what you expected but ...

Code: Pascal  [Select][+][-]
  1.   ...
  2.   function QtyAsString: string;
  3.   function QtyAsInteger: Integer;
  4.   ...
  5.  
  6. var
  7.   A: integer;
  8.   B: string;
  9.  
  10. begin
  11.   A :=10;
  12.   Test.Qty := A;
  13.  
  14.   B := Test.QtyAsString;
  15. end;
  16.  

those are just ideas, maybe you already thought about those approaches.

 

TinyPortal © 2005-2018