views:

100

answers:

2
+1  Q: 

Set Of String??!!

You are familiar with this block:

Var
  mySet: Set Of Char;
  C: Char;
begin
  mySet := ['a', 'b', 'c'];
  If C In mySet Then ShowMessage('Exists');
end;

Is there any way to declare Set Of STRING? or is there similar code that i can use instead? The important part of this block is If C In mySet Then ShowMessage('Exists'); I wanna use something like this about a set of string.
Thanks.

+5  A: 

Sets are implemented using bit arrays. So no, you cannot have a 'set of string'. Use a TStringList instead, ie:

var 
  mySet: TStringList;
  S: String;
begin 
  S := ...;
  mySet := TStringList.Create;
  try
    mySet.Add('a');
    mySet.Add('b');
    mySet.Add('c'); 
    if mySet.IndexOf(S) <> -1 Then ShowMessage('Exists');
  finally
    mySet.Free;
  end;
end; 
Remy Lebeau - TeamB
If you have a lot of strings, it could be better to use sorted list and ignore duplicates:mySet.Sorted := True;mySet.Duplicates := dupIgnores;
Harriv
+1  A: 

You can make use of this.

type 
  TAnyEnum = (aeVal1, aeVal2, aeVal3);
  TEnuns = set of TAnyEnum;
  TAnyMessages: array [TAnyEnum] of String;

const 
  MyMessages: TAnyMessages = ('Exists', 'Something else', 'WTF!?');

var
  MySet : TEnums;
begin
  MySet = [aeVal1, aeVal2];
  If aeVal1 in MySet then ShowMessage(MyMessages[aeVal1]);
end;
Fabricio Araujo
Thank you, Useful!!
Armin