Dica para trabalhar com arrays no delphi, pegando a conversão de cada um em string
Definição dos tipos
1
2
3
4
| type
TType = (mtMenuFaturamento, mtMenuRelatorios);
const
StrType: array [TType] of string = ('MENU FATURAMENTO', 'MENU RELATÓRIOS'); |
type
TType = (mtMenuFaturamento, mtMenuRelatorios);
const
StrType: array [TType] of string = ('MENU FATURAMENTO', 'MENU RELATÓRIOS');
USANDO RTTI
1
2
3
4
5
6
7
8
9
10
11
12
| uses System.Rtti;
...
procedure TForm1.BtnComRTTIClick(Sender: TObject);
var
S: string;
I: Integer;
begin
I := Edit1.Value;
S := TRttiEnumerationType.GetName(TType(I));
ShowMessage('Tipo: ' + S + sLineBreak + 'Str : ' + StrType[TType(I)]);
end; |
uses System.Rtti;
...
procedure TForm1.BtnComRTTIClick(Sender: TObject);
var
S: string;
I: Integer;
begin
I := Edit1.Value;
S := TRttiEnumerationType.GetName(TType(I));
ShowMessage('Tipo: ' + S + sLineBreak + 'Str : ' + StrType[TType(I)]);
end;
SEM USAR RTTI
1
2
3
4
5
6
7
8
9
10
11
12
13
| uses System.TypInfo
...
procedure TForm1.BtnSemRTTIClick(Sender: TObject);
var
S: string;
I: Integer;
begin
I := Edit1.Value;
S := GetEnumName(TypeInfo(TType), I);
ShowMessage(
'Tipo: ' + S + sLineBreak +
'Str : ' + StrType[TType(GetEnumValue(TypeInfo(TType),S))]);
end; |
uses System.TypInfo
...
procedure TForm1.BtnSemRTTIClick(Sender: TObject);
var
S: string;
I: Integer;
begin
I := Edit1.Value;
S := GetEnumName(TypeInfo(TType), I);
ShowMessage(
'Tipo: ' + S + sLineBreak +
'Str : ' + StrType[TType(GetEnumValue(TypeInfo(TType),S))]);
end;
Para facilitar o entendimento, disponibilizamos um vídeo com a aplicação da dica.
Vídeo demostrando o uso de tipos enumerados com strings
Dúvidas ou sugestões, deixe o seu comentário.
-
Ivan Cesar
-
8.002 views
- 0 comentários
- 31 de maio de 2018
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo