DXE:
I think that Tdicionary key values are case sensitive - which is a bit of a
surprise.
Though I can cope with it, it there a property that I missed to make them
case-insensitive?
Tom
Tom deNeef wrote:
> DXE:
> I think that Tdicionary key values are case sensitive - which is a
> bit of a surprise.
It is case sensitive.
> Though I can cope with it, it there a property
> that I missed to make them case-insensitive?
No there isn't a property, because it is a general purpose dictionary.
What you can do is to implement a case insensitive version of the
IEqualityComparer interface.
{code}
program Project109;
{$APPTYPE CONSOLE}
uses
SysUtils, Generics.Collections, Generics.Defaults;
type
TStringEqualityComparer = class(TInterfacedObject,
IEqualityComparer)
function Equals(const Left, Right: string): Boolean;
function GetHashCode(const Value: string): Integer;
end;
{ TStringEqualityComparer }
function TStringEqualityComparer.Equals(const Left, Right: string):
Boolean;
begin
Result := CompareText(Left, Right) = 0;
end;
function TStringEqualityComparer.GetHashCode(const Value: string):
Integer;
var
S: string;
begin
S := AnsiLowerCase(Value);
Result := BobJenkinsHash(S[1], Length(S) * SizeOf(S[1]), 0);
end;
var
D: TDictionary;
begin
D := TDictionary.Create
(TStringEqualityComparer.Create);
try
D.Add('A', 1);
{ should raise EListError with message 'Duplicates not allowed'
when case insensitive }
D.Add('a', 2);
D.Add('B', 3);
finally
D.Free;
end;
end.
{code}
--
Uwe Schuster
http://www.bitcommander.de/blog
"Uwe Schuster" schreef in bericht
news:379218@forums.embarcadero.com...
> Tom deNeef wrote:
>
>> DXE:
>> I think that Tdicionary key values are case sensitive - which is a
>> bit of a surprise.
>
> It is case sensitive.
>
>> Though I can cope with it, it there a property
>> that I missed to make them case-insensitive?
>
> No there isn't a property, because it is a general purpose dictionary.
> What you can do is to implement a case insensitive version of the
> IEqualityComparer interface.
>
> {code}
> program Project109;
>
> {$APPTYPE CONSOLE}
>
> uses
> SysUtils, Generics.Collections, Generics.Defaults;
>
> type
> TStringEqualityComparer = class(TInterfacedObject,
> IEqualityComparer)
> function Equals(const Left, Right: string): Boolean;
> function GetHashCode(const Value: string): Integer;
> end;
>
> { TStringEqualityComparer }
>
> function TStringEqualityComparer.Equals(const Left, Right: string):
> Boolean;
> begin
> Result := CompareText(Left, Right) = 0;
> end;
>
> function TStringEqualityComparer.GetHashCode(const Value: string):
> Integer;
> var
> S: string;
> begin
> S := AnsiLowerCase(Value);
> Result := BobJenkinsHash(S[1], Length(S) * SizeOf(S[1]), 0);
> end;
>
> var
> D: TDictionary;
> begin
> D := TDictionary.Create
> (TStringEqualityComparer.Create);
> try
> D.Add('A', 1);
>
> { should raise EListError with message 'Duplicates not allowed'
> when case insensitive }
> D.Add('a', 2);
>
> D.Add('B', 3);
> finally
> D.Free;
> end;
> end.
> {code}
Thank you. This will move me into a part of the laguage that I have no
experience with.
I will define a descendant of my particular Tdictionary and override the Add
and tryGetValue methods (where the strings will be transformed to
uppercase).
Tom
> Thank you. This will move me into a part of the laguage that I have no
> experience with.
> I will define a descendant of my particular Tdictionary and override the Add
> and tryGetValue methods (where the strings will be transformed to uppercase).
> Tom
haha like me :)
by the way we have to learn..