为什么Delphi 2009不让我在集合中包含一个字符? [英] Why won't Delphi 2009 let me Include a Char in a set?
问题描述
这是有关将旧代码转换为D2009和Unicode的另一个问题。我敢肯定,这很简单,但是我看不到解决方案...
CharacterSet是一组Char,而s [i]也应该是Char。
但是编译器仍然认为AnsiChar和Char之间存在冲突。
Here is another question about convert old code to D2009 and Unicode. I'm certain that there is simple but i don't see the solution... CharacterSet is a set of Char and s[i] should also be a Char. But the compiler still think there is a conflict between AnsiChar and Char.
代码:
TSetOfChar = Set of Char;
procedure aFunc;
var
CharacterSet: TSetOfChar;
s: String;
j: Integer;
CaseSensitive: Boolean;
begin
// Other code that assign a string to s
// Set CaseSensitive to a value
CharacterSet := [];
for j := 1 to Length(s) do
begin
Include(CharacterSet, s[j]); // E2010 Incompatible types: 'AnsiChar' and 'Char'
if not CaseSensitive then
begin
Include(CharacterSet, AnsiUpperCase(s[j])[1]);
Include(CharacterSet, AnsiLowerCase(s[j])[1])
end
end;
end;
推荐答案
这个问题没有好的简单答案(原因已经由梅森(Mason)给出。好的解决方案是重新考虑算法以摆脱字符集类型。快速而肮脏的解决方案是保留ansi字符和字符串:
There is no good and simple answer to the question (the reason is already given by Mason). The good solution is to reconsider the algoritm to get rid off "set of char" type. The quick and dirty solution is to preserve ansi chars and strings:
TSetOfChar = Set of AnsiChar;
procedure aFunc;
var
CharacterSet: TSetOfChar;
s: String;
S1, SU, SL: Ansistring;
j: Integer;
CaseSensitive: Boolean;
begin
// Other code that assign a string to s
// Set CaseSensitive to a value
S1:= s;
SU:= AnsiUpperCase(s);
SL:= AnsiLowerCase(s);
CharacterSet := [];
for j := 1 to Length(S1) do
begin
Include(CharacterSet, S1[j]);
if not CaseSensitive then
begin
Include(CharacterSet, SU[j]);
Include(CharacterSet, SL[j]);
end
end;
end;
这篇关于为什么Delphi 2009不让我在集合中包含一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!