Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
General
»
Overloading operator IN
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
How to compare floating p...
by
creaothceann
[
Today
at 02:14:30 pm]
Getting name / line of ca...
by
Thaddy
[
Today
at 01:58:46 pm]
Label with multiple lines...
by
paweld
[
Today
at 12:39:49 pm]
[Solved] TStringGrid disp...
by
jcmontherock
[
Today
at 11:30:47 am]
FPC Unleashed (inline var...
by
LeP
[
Today
at 11:25:41 am]
Pascal port of Unishox2
by
AlexTP
[
Today
at 10:29:01 am]
Qt does not position form...
by
zeljko
[
Today
at 09:05:06 am]
P.I.S.S. a PlugIn-framewo...
by
cdbc
[
Today
at 07:59:58 am]
Limiting Search in TSelec...
by
dsiders
[
Today
at 05:29:01 am]
using make with a differe...
by
Thausand
[
Today
at 12:04:50 am]
RunFormula: math expressi...
by
valdir.marcos
[May 10, 2026, 10:49:20 pm]
Help with FPReport data b...
by
Dankan1890
[May 10, 2026, 10:08:22 pm]
Scripts to generate ofici...
by
marcov
[May 10, 2026, 09:28:54 pm]
How do 'with' statements ...
by
Warfley
[May 10, 2026, 09:13:18 pm]
FpDebug for Linux AARCH64
by
Martin_fr
[May 10, 2026, 08:21:58 pm]
Application.QueueAsyncCal...
by
Thaddy
[May 10, 2026, 03:17:23 pm]
Windows API Hooking/DLL I...
by
Thaddy
[May 10, 2026, 01:08:56 pm]
TComboBoxEx & TCheckCombo...
by
Paolo
[May 10, 2026, 11:28:00 am]
DataPort or Synpase stat...
by
serbod
[May 10, 2026, 09:30:51 am]
SynEdit auto End of line ...
by
Martin_fr
[May 10, 2026, 09:08:36 am]
Show Case: WritingTool
by
ginoo
[May 10, 2026, 08:47:02 am]
How to view the TODO list...
by
Thaddy
[May 10, 2026, 07:16:27 am]
International Pascal Cong...
by
Mike.Cornflake
[May 09, 2026, 06:45:37 pm]
SpreadSheet Add Chart Lin...
by
eldonfsr
[May 09, 2026, 06:19:33 pm]
How to access inherited m...
by
Thaddy
[May 09, 2026, 04:12:38 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Overloading operator IN (Read 715 times)
domasz
Hero Member
Posts: 629
Overloading operator IN
«
on:
April 12, 2025, 07:20:25 am »
I want something like this:
Code: Pascal
[Select]
[+]
[-]
var
hey
:
String
;
begin
hey
:
=
'one'
;
if
hey
in
[
'one'
,
'two'
]
then
Exit
;
I tried:
Code: Pascal
[Select]
[+]
[-]
operator
in
(
A
:
String
;
const
B
:
array
of
const
)
:
boolean
;
begin
Result
:
=
False
;
end
;
Not ideal solution:
Code: Pascal
[Select]
[+]
[-]
function
Group
(
const
Ar
:
array
of
String
)
:
TStringArray
;
var
i
:
Integer
;
begin
SetLength
(
Result
,
High
(
Ar
)
+
1
)
;
for
i
:
=
0
to
High
(
Ar
)
do
Result
[
i
]
:
=
Ar
[
i
]
;
end
;
operator
in
(
A
:
String
;
B
:
TStringArray
)
:
Boolean
;
var
i
:
Integer
;
begin
for
i
:
=
0
to
Length
(
B
)
-
1
do
if
B
[
i
]
=
A
then
Exit
(
True
)
;
Result
:
=
False
;
end
;
if
hey
in
Group
(
[
'one'
,
'two'
]
)
How do I overload IN operator?
«
Last Edit: April 12, 2025, 07:31:37 am by domasz
»
Logged
cdbc
Hero Member
Posts: 2771
Re: Overloading operator IN
«
Reply #1 on:
April 12, 2025, 07:33:59 am »
Hi
Nah, instead you should use 'IndexStr' or 'IndexText' from 'StrUtils', in a case statement, like this:
Code: Pascal
[Select]
[+]
[-]
var
hey
:
String
;
begin
hey
:
=
'one'
;
case
IndexText
(
hey
,
[
'one'
,
'two'
,
'three'
]
)
of
0
:
exit
;
// 'one'
1
:
Caption
:
=
'Two'
;
2
:
showmessage
(
'Three'
)
;
end
;
end
;
/// edited later ///
or
case
IndexStr
(
hey
,
Group
)
of
0
:
/// your stuff
...
end
;
Regards Benny
«
Last Edit: April 12, 2025, 07:39:47 am by cdbc
»
Logged
If it ain't broke, don't fix it
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release & FPC Main -> Lazarus Main
domasz
Hero Member
Posts: 629
Re: Overloading operator IN
«
Reply #2 on:
April 12, 2025, 07:42:31 am »
Thanks, Benny but that looks terrible. I want something pretty
Logged
cdbc
Hero Member
Posts: 2771
Re: Overloading operator IN
«
Reply #3 on:
April 12, 2025, 07:50:21 am »
Hi
No worries, but be warned: 'array of const' is a whole other can of worms to open... But Hey, it's your party man...
Regards Benny
Logged
If it ain't broke, don't fix it
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release & FPC Main -> Lazarus Main
domasz
Hero Member
Posts: 629
Re: Overloading operator IN
«
Reply #4 on:
April 12, 2025, 08:06:37 am »
Code: Pascal
[Select]
[+]
[-]
function
Group
(
A
:
String
;
B
:
String
=
''
;
C
:
String
=
''
;
D
:
String
=
''
;
E
:
String
=
''
;
F
:
String
=
''
;
G
:
String
=
''
;
H
:
String
=
''
;
I
:
String
=
''
;
J
:
String
=
''
)
:
TStringArray
;
var
ii
:
Integer
;
Ar
:
TStringArray
;
Len
:
Integer
;
begin
Ar
:
=
TStringArray
.
Create
(
A
,
B
,
C
,
D
,
E
,
F
,
G
,
H
,
I
,
J
)
;
for
ii
:
=
9
downto
1
do
begin
if
Ar
[
ii
]
<>
''
then
begin
Len
:
=
ii
+
1
;
break
;
end
;
end
;
SetLength
(
Result
,
Len
)
;
for
ii
:
=
0
to
Len
-
1
do
Result
[
ii
]
:
=
Ar
[
ii
]
;
end
;
operator
in
(
A
:
String
;
B
:
TStringArray
)
:
Boolean
;
var
i
:
Integer
;
begin
for
i
:
=
0
to
Length
(
B
)
-
1
do
if
B
[
i
]
=
A
then
Exit
(
True
)
;
Result
:
=
False
;
end
;
Code: Pascal
[Select]
[+]
[-]
if
hey
in
Group
(
'one'
,
'two'
)
then
It's slow but isn't it pretty?
«
Last Edit: April 12, 2025, 08:48:49 am by domasz
»
Logged
egsuh
Hero Member
Posts: 1789
Re: Overloading operator IN
«
Reply #5 on:
April 12, 2025, 10:42:27 am »
Why not following? This works.
Code: Pascal
[Select]
[+]
[-]
operator
in
(
a
:
string
;
b
:
array
of
string
)
:
boolean
;
implementation
operator
in
(
a
:
string
;
b
:
array
of
string
)
:
boolean
;
var
c
:
string
;
begin
Result
:
=
false
;
for
c
in
b
do
if
a
=
c
then
Exit
(
true
)
;
end
;
{$R *.lfm}
{ TForm1 }
procedure
TForm1
.
Button1Click
(
Sender
:
TObject
)
;
begin
showmessage
(
booltostr
(
'this'
in
TStringArray
(
[
'this'
,
'that'
]
)
,
true
)
)
;
end
;
Logged
domasz
Hero Member
Posts: 629
Re: Overloading operator IN
«
Reply #6 on:
April 12, 2025, 11:57:21 am »
Because it's a Fiat Multipla to my Porsche
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
General
»
Overloading operator IN
TinyPortal
© 2005-2018