tags:

views:

125

answers:

2

I have a set of enumeration values that I need to convert to text and then back to a set.

I believe that GetSetProp and SetSetProp from TypInfo unit would allow to do this but I have no idea on to get it to work. Any idea on how I can use GetSetProp and SetSetProp to accomplish this?

type
  TSomething = (sOne, sTwo, sThree, sFour, s Five);
  TSomethings = set of TSomething;

var
  Something: TSomethings;
  s: string;
...
  Something := [sOne, sThree];

  s := GetSetProp(????);

  Something := [];
  // then use SetSetProp to set Something back to [sOne, sThree]
  Something := ????
+2  A: 

As the method name might lead: this works only for published properties!

type
  TSomething = (sOne, sTwo, sThree, sFour, sFive);
  TSomethings = set of TSomething;
  TSomeClass = class
  private
    FSomeThing: TSomethings;
  public
  published
    property SomeThing: TSomethings read FSomeThing write FSomeThing;
  end;

...
var
  SomeClass: TSomeClass;
  s: string;
begin
  SomeClass := TSomeClass.Create;
  try
    SomeClass.Something := [sOne, sThree];
    s := GetSetProp(SomeClass, 'Something');
    ...
  finally
    SomeClass.Free;
  end;
Uwe Raabe
Unfortunately I don't (and can't) have the set on the published properties, so I can't use this code. I decided to accept the answer from Jeroen as it does exactly what I need but I would like to thank you for the time taken to post your answer.
smartins
+4  A: 

This great post on SetToString, StringToSet by tondrej solves your problem.

--jeroen

Jeroen Pluimers