Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] „Error: Constant and CASE types do not match‟ with string
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
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
what's difference between...
by
ASerge
[
Today
at 07:09:52 pm]
splitting an image
by
KodeZwerg
[
Today
at 07:07:21 pm]
Debugger error
by
bobonwhidbey
[
Today
at 07:03:07 pm]
How to: create DLL file f...
by
paule32
[
Today
at 07:02:31 pm]
System.Now gives timestam...
by
alpine
[
Today
at 06:58:15 pm]
Demo Scene Bitmap Font Sc...
by
circular
[
Today
at 06:53:32 pm]
Your best UI design - con...
by
circular
[
Today
at 06:48:12 pm]
Web Applications with Pas...
by
1HuntnMan
[
Today
at 06:44:00 pm]
Converting a string/index...
by
1HuntnMan
[
Today
at 06:35:19 pm]
Drawing grid lines on a t...
by
KodeZwerg
[
Today
at 05:42:19 pm]
Compile/Convert Delphi pr...
by
Martin_fr
[
Today
at 05:29:03 pm]
Lazarus features in a non...
by
Lutz Mändle
[
Today
at 05:26:34 pm]
Lazarus for Windows on aa...
by
msintle
[
Today
at 05:03:57 pm]
AI, NLP and CAI: Text Gen...
by
indydev
[
Today
at 04:51:00 pm]
Parameter passing odditie...
by
KodeZwerg
[
Today
at 03:41:49 pm]
Surf Units in Text Mode I...
by
mosquito
[
Today
at 01:58:21 pm]
Ho Ho Ho IntraWeb in Laza...
by
Thaddy
[
Today
at 01:55:52 pm]
Generics - correct syntax
by
Thaddy
[
Today
at 01:48:32 pm]
FpDebug questions
by
440bx
[
Today
at 01:22:53 pm]
leetcode doesn’t support ...
by
codeninja
[
Today
at 01:16:43 pm]
Can function be used for ...
by
Joanna
[
Today
at 12:48:19 pm]
Access violation when ope...
by
Чебурашка
[
Today
at 12:14:30 pm]
Access violation when re-...
by
Chris Osborne
[
Today
at 11:43:49 am]
how to tweek fpmake to co...
by
marcov
[
Today
at 10:35:11 am]
Why isn't TTIRadioGroup's...
by
egsuh
[
Today
at 08:09:35 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] „Error: Constant and CASE types do not match‟ with string (Read 8949 times)
CM630
Hero Member
Posts: 1091
Не съм сигурен, че те разбирам.
[SOLVED] „Error: Constant and CASE types do not match‟ with string
«
on:
July 09, 2018, 03:31:48 pm »
Following code works right:
Code: Pascal
[Select]
[+]
[-]
procedure
TForm1
.
Button1Click
(
Sender
:
TObject
)
;
var
A
:
string
=
''
;
B
:
string
=
'B'
;
C
:
string
=
'C'
;
begin
A
:
=
B
;
case
A
of
'B'
:
ShowMessage
(
B
)
;
'C'
:
ShowMessage
(
C
)
;
end
;
end
;
But this code generates „Error: Constant and CASE types do not match‟.
Code: Pascal
[Select]
[+]
[-]
procedure
TForm1
.
Button1Click
(
Sender
:
TObject
)
;
var
A
:
string
=
''
;
B
:
string
=
'B'
;
C
:
string
=
'C'
;
begin
A
:
=
B
;
case
A
of
B
:
ShowMessage
(
B
)
;
C
:
ShowMessage
(
C
)
;
end
;
end
;
B and C are strings. I tryed to cast to cast them to strings, anyway, but still I get the same error?
Is it me doing sth. wrong?
«
Last Edit: July 09, 2018, 04:00:45 pm by CM630
»
Logged
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.
RayoGlauco
Full Member
Posts: 179
Beers: 1567
Re: „Error: Constant and CASE types do not match‟ with string
«
Reply #1 on:
July 09, 2018, 03:49:09 pm »
In the second example, you use variables as selectors, instead of constants (B is a variable; 'B' is a constant). You cannot use variables as selectors.
See
http://wiki.freepascal.org/Case
Logged
To err is human, but to really mess things up, you need a computer.
Thaddy
Hero Member
Posts: 14391
Sensorship about opinions does not belong here.
Re: „Error: Constant and CASE types do not match‟ with string
«
Reply #2 on:
July 09, 2018, 03:50:04 pm »
Needs a literal value, indeed.
Logged
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.
Gammatester
Jr. Member
Posts: 69
Re: „Error: Constant and CASE types do not match‟ with string
«
Reply #3 on:
July 09, 2018, 03:50:20 pm »
You should read the second part of the error message 'Error: String expression expected'. The case labels must be constants (i.e. literals or true constants). Your construct would throw equivalent message even for integer variables. The following code does work:
Code: Pascal
[Select]
[+]
[-]
var
A
:
string
=
''
;
const
B
=
'B'
;
C
=
'C'
;
begin
A
:
=
B
;
case
A
of
B
:
writeln
(
B
)
;
C
:
writeln
(
C
)
;
end
;
end
;
Logged
CM630
Hero Member
Posts: 1091
Не съм сигурен, че те разбирам.
Re: „Error: Constant and CASE types do not match‟ with string
«
Reply #4 on:
July 09, 2018, 03:57:15 pm »
Ok, thanks, I will rewrite it with
Quote
If... then.
Logged
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] „Error: Constant and CASE types do not match‟ with string
TinyPortal
© 2005-2018