Recent

Author Topic: IfThenStr does not work - omg  (Read 1693 times)

paule32

  • Sr. Member
  • ****
  • Posts: 383
IfThenStr does not work - omg
« on: March 15, 2025, 11:07:49 am »
Hello,

given following Code:

Code: Pascal  [Select][+][-]
  1. function IfThenStr(Condition: Boolean; TrueValue, FalseValue: String): String;
  2. begin
  3.   if Condition then Result := TrueValue else Result := FalseValue;
  4. end;
  5.  
  6. function foo(AString: String): String;
  7. begin
  8.   result := 'bar';
  9. end;

then I get Compiler Error - Boolean expected for Parameter 2 by using:

Code: Pascal  [Select][+][-]
  1. checkList.Checked[1] := ifThenStr(
  2.   checkList.Items[1] = foo('All Header: ON'),
  3.   foo('All Header: OFF'),
  4.   foo('All Header: ON')
  5. );

do I something missing ?
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Ally

  • Jr. Member
  • **
  • Posts: 62
Re: IfThenStr does not work - omg
« Reply #1 on: March 15, 2025, 11:32:47 am »
Hello paule32,

Does it work if you enclose the first parameter in brackets?

Code: Pascal  [Select][+][-]
  1. checkList.Checked[1] := ifThenStr((checkList.Items[1] = foo('All Header: ON')), foo('All Header: OFF'), foo('All Header: ON'));

Greetings
Roland

wp

  • Hero Member
  • *****
  • Posts: 12757
Re: IfThenStr does not work - omg
« Reply #2 on: March 15, 2025, 11:48:22 am »
Code: Pascal  [Select][+][-]
  1. checkList.Checked[1] := ifThenStr(
  2.   checkList.Items[1] = foo('All Header: ON'),
  3.   foo('All Header: OFF'),
  4.   foo('All Header: ON')
  5. );
CheckList.Checked[AIndex] is a boolean property. But you assign to it a string (IfThenStr returns a string).

paule32

  • Sr. Member
  • ****
  • Posts: 383
Re: IfThenStr does not work - omg
« Reply #3 on: March 15, 2025, 12:10:07 pm »
@Ally:
don't.
mainform.pas(393,28) Error: Incompatible type for arg no. 2: Got "AnsiString", expected "Boolean"

@wp:
For me, it makes no sense. Because first Parameter (1) is a Boolean.
The second (2) and third (3) Parameters are Strings.
The return Value is a String.

checkList.Items[index] is TCheckListBox.Items[index]
.Items is a TStringList.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Nimbus

  • New Member
  • *
  • Posts: 28
Re: IfThenStr does not work - omg
« Reply #4 on: March 15, 2025, 12:28:16 pm »
Hi paule32,

The problem is not the function itself, but what you are trying to do with its result.
The result of the function is a String (AnsiString). And you are trying to asign it to TCheckListBox.Checked[] property, which is of Boolean type.
You can not assign a String to a Boolean, hence the error.

paule32

  • Sr. Member
  • ****
  • Posts: 383
Re: IfThenStr does not work - omg
« Reply #5 on: March 15, 2025, 12:32:12 pm »
omg - blame on me...

you are right.
Checked is a Boolean

say: I am a GNU  :o
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

cdbc

  • Hero Member
  • *****
  • Posts: 2084
    • http://www.cdbc.dk
Re: IfThenStr does not work - omg
« Reply #6 on: March 15, 2025, 01:05:06 pm »
Hi
What is GNU (G)eneral brain (N)ot (U)sed ?!?  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

paule32

  • Sr. Member
  • ****
  • Posts: 383
Re: IfThenStr does not work - omg
« Reply #7 on: March 15, 2025, 01:07:25 pm »
yes, indeed. 100 Points !  :'(
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8364
Re: IfThenStr does not work - omg
« Reply #8 on: March 15, 2025, 02:23:06 pm »
omg - blame on me...

you are right.
Checked is a Boolean

But at the same time, the error message isn't very helpful: at an absolute minimum you have to understand that Checked[] is a property which is implemented by something like SetChecked() with an object reference as a hidden first parameter.

Did your original error message- which you did not reproduce in full- give any useful hint in terms of line and column number?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 16783
  • Ceterum censeo Trump esse delendam
Re: IfThenStr does not work - omg
« Reply #9 on: March 15, 2025, 04:43:13 pm »
given following Code:

Code: Pascal  [Select][+][-]
  1. function IfThenStr(Condition: Boolean; TrueValue, FalseValue: String): String;
  2.  
You are missing that there is a generic ifthen that you can specialize to string.
I wrote that seven tears ago.

It also solves your problem.
« Last Edit: March 15, 2025, 04:47:51 pm by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Thaddy

  • Hero Member
  • *****
  • Posts: 16783
  • Ceterum censeo Trump esse delendam
Re: IfThenStr does not work - omg
« Reply #10 on: March 16, 2025, 09:16:30 am »
Here's the test code I added 7 years ago:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses sysutils;
  3. type
  4.   Ttest = function:string;
  5.  
  6. function SomeTestOne:string;
  7. begin
  8.  writestr(result,'Test1');
  9. end;
  10.  
  11. function SomeTestTwo:string;
  12. begin
  13.  writestr(result,'Test2')
  14. end;  
  15.  
  16. var
  17.   a:integer = 100;
  18.   b:integer = 200;
  19.   c:single = 0.001;
  20.   d:single = 1.000;
  21.   e:TTest = SomeTestOne;
  22.   f:TTest = SomeTestTwo;
  23. begin
  24.   writeln(ifthen<integer>(true,a,b));
  25.   writeln(ifthen<integer>(false,a,b));
  26.   writeln(ifthen<single>(true,c,d):2:3);
  27.   writeln(ifthen<single>(false,c,d):2:3);
  28.   writeln(ifthen<Ttest>(true,e,f));  // executes e
  29.   writeln(ifthen<Ttest>(false,e,f)); // executes f
  30.   writeln(ifthen<string>(true,'true','false'));
  31.   writeln(ifthen<string>(false,'true','false'));  
  32. end.
This is in 3.2.0 and higher.
In your case, look at the last 4 examples.
« Last Edit: March 16, 2025, 09:19:18 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8364
Re: IfThenStr does not work - omg
« Reply #11 on: March 16, 2025, 09:18:40 am »
You are missing that there is a generic ifthen that you can specialize to string.
I wrote that seven tears ago.

Documented where?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 16783
  • Ceterum censeo Trump esse delendam
Re: IfThenStr does not work - omg
« Reply #12 on: March 16, 2025, 09:24:02 am »
Good point. There is documentation for it, including something close to above example, but this feature somehow did not make it to current documentation (although Michael made the actual commit).

Issue #34012, closed and commited.

(The generic randomfrom I mentioned in the comments is probably also not documented)

I guess those patches were too trivial. :'( but most of my patches made it to documentation.
« Last Edit: March 16, 2025, 10:02:03 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Nimbus

  • New Member
  • *
  • Posts: 28
Re: IfThenStr does not work - omg
« Reply #13 on: March 16, 2025, 12:44:53 pm »
I'm not sure the TS needs this advice in any way, but note there's also non-generic version in StrUtils, which has been there for ages, I believe

https://www.freepascal.org/docs-html/rtl/strutils/ifthen.html

Thaddy

  • Hero Member
  • *****
  • Posts: 16783
  • Ceterum censeo Trump esse delendam
Re: IfThenStr does not work - omg
« Reply #14 on: March 16, 2025, 01:08:25 pm »
That was already mentioned.
Note that he actually executes function parameters and does not pass simple strings. Hence the generic is actually better suited for his problem because it is not restricted to returning a string.
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

 

TinyPortal © 2005-2018