Conjuntos (set)
Gostaria de saber se álguem sabe como fazer uma variável tipo String receber um determinado valor de um conjunto. Segue o código abaixo para melhor entendimento.
[b:bdeb4f9030]Var[/b:bdeb4f9030]
x: [color=red:bdeb4f9030][b:bdeb4f9030]set of[/b:bdeb4f9030][/color:bdeb4f9030] 0..5;
a: [b:bdeb4f9030]string[/b:bdeb4f9030];
[b:bdeb4f9030]begin[/b:bdeb4f9030]
x:= [5];
a:= TypeCast [b:bdeb4f9030]????[/b:bdeb4f9030] x[[b:bdeb4f9030]?[/b:bdeb4f9030]];
[b:bdeb4f9030]end;[/b:bdeb4f9030]
Minhas dúvidas são estas, apenas quero saber se tem como uma variável receber um determinado valor de um Set.
Desde já agradeço.
Cirolaman
[b:bdeb4f9030]Var[/b:bdeb4f9030]
x: [color=red:bdeb4f9030][b:bdeb4f9030]set of[/b:bdeb4f9030][/color:bdeb4f9030] 0..5;
a: [b:bdeb4f9030]string[/b:bdeb4f9030];
[b:bdeb4f9030]begin[/b:bdeb4f9030]
x:= [5];
a:= TypeCast [b:bdeb4f9030]????[/b:bdeb4f9030] x[[b:bdeb4f9030]?[/b:bdeb4f9030]];
[b:bdeb4f9030]end;[/b:bdeb4f9030]
Minhas dúvidas são estas, apenas quero saber se tem como uma variável receber um determinado valor de um Set.
Desde já agradeço.
Cirolaman
Cirolaman
Curtidas 0
Respostas
Beppe
25/01/2004
Como você quer que apareça na string? Não tem typecast, mas é só escrever uma função.
Não testei, fiz agora. Então diz se funcionar, ok?
type TByteSet = set of Byte; function ByteSetToString(const Bits: TByteSet): String; var I: Integer; begin with TStringStream.Create() do try WriteString(´[´); I := 0; repeat while (I <= 255) and not (I in Bits) do Inc(I); if I > 255 then Exit; if Size > 1 then WriteString(´, ´); WriteString(IntToStr(I)); if (I < 255) and (Succ(I) in Bits) then begin WriteString(IntToStr(I)); while (I <= 255) and (I in Bits) do Inc(I); WriteString(IntToStr(Pred(I))); end; until False; WriteString(´]´); finally Result := DataString; Free; end; end;
Não testei, fiz agora. Então diz se funcionar, ok?
GOSTEI 0
Aroldo Zanela
25/01/2004
Colega,
Se eu entendi o que você quer, então é melhor utilizar um Array ao invés de conjunto.
Se eu entendi o que você quer, então é melhor utilizar um Array ao invés de conjunto.
Var X: array[0..5] of Integer; A: String; begin X[1] := 255; A := intToStr(x[1]); ShowMessage(A); // Retorna 255 end;
GOSTEI 0
Cirolaman
25/01/2004
Beppe.
Valeu. Mas não funcionou, quando rodo a aplicação da pau e trava.
Cirolaman.
Valeu. Mas não funcionou, quando rodo a aplicação da pau e trava.
Cirolaman.
GOSTEI 0
Beppe
25/01/2004
Putz, foi um errinho de lógica, o que que um Inc faltando não atrapalha :oops:
type TByteSet = set of Byte; function ByteSetToString(const Bits: TByteSet): String; var I: Integer; begin with TStringStream.Create(´´) do try WriteString(´[´); I := 0; repeat while (I <= 255) and not (I in Bits) do Inc(I); if I > 255 then Break; if Size > 1 then WriteString(´, ´); WriteString(IntToStr(I)); if (I < 255) and (Succ(I) in Bits) then begin WriteString(´..´); while (I <= 255) and (I in Bits) do Inc(I); WriteString(IntToStr(Pred(I))); end else Inc(I); until False; WriteString(´]´); finally Result := DataString; Free; end; end;
Caption := ByteSetToString([0, 3..8, 10..12, 14]);
GOSTEI 0