Purpose and Usage of CharInSet function in Delphi XE4
CharInSet function is found in SysUtils unit. When I was migrating my Delphi 7 code to Delphi XE4, I found the usage and purpose of CharInSet function. While migrating my code from Delphi 7 to Delphi XE4, I got following compiler warning:
[DCC Warning] MyUnit.pas(80): W1050 WideChar reduced to byte char in set expressions. Consider using 'CharInSet' function in 'SysUtils' unit.
I had following procedure in my MyUnit.pas file which was throwing this compiler warning:
procedure MyProcedure;
var
C: Char;
begin
C := 'k';
if C in ['a'..'z', 'A'..'Z'] then
begin
ShowMessage("Show any message");
end;
end;
Cause of the compiler warning: In Delphi 7, a character was one byte, so holding characters in a set was no problem. But now in Delphi XE4, Char is declared as a WideChar, and thus cannot be held in a set any longer.
Solution of the above compiler warning: If you don't bother about this compiler warning, you can ignore this. But if you actually want to get rid of this compiler warning, you have to use CharInSet function to remove this compiler warning like following:
if CharInSet(C, ['a'..'z', 'A'..'Z']) then
begin
ShowMessage("Show any message");
end;
The CharInSet function will return a Boolean value, and compile without the compiler warning.
No comments:
Post a Comment