GARANTIR DESCONTO

Fórum Conjuntos (set) #209245

25/01/2004

0

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


Cirolaman

Cirolaman

Responder

Posts

25/01/2004

Beppe

Como você quer que apareça na string? Não tem typecast, mas é só escrever uma função.

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?


Responder

Gostei + 0

25/01/2004

Aroldo Zanela

Colega,

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;



Responder

Gostei + 0

26/01/2004

Cirolaman

Beppe.

Valeu. Mas não funcionou, quando rodo a aplicação da pau e trava.

Cirolaman.


Responder

Gostei + 0

26/01/2004

Beppe

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]);



Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar