GetEnumName TypeInfo problems (pascal / Delphi console)

旧街凉风 提交于 2020-01-03 20:58:09

问题


Working on a console application using Delphi 7, and have run into a problem. I get an error on line 26 after

str := GetEnumName(TypeInfo (words[3].group),

The error reads "[Error] Project1.dpr(26): TYPEINFO standard function expects a type identifier" if anyone could help with this, it would be a great help!

Cheers!

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  TypInfo;

type
  wordset = Record
    word  : String;
    group : (flavour, colour, place, animal);
  end;
Var
  words : Array [1..50] of wordset;
  str : string;
  groups: string;
Begin
  words[1].word  := 'chocolate';
  words[1].group := flavour;
  words[2].word  := 'vanilla';
  words[2].group := flavour;
  words[3].word  := 'strawberry';
  words[3].group := flavour;

  str := GetEnumName(TypeInfo (words[3].group), integer(group));

  readln;
end.

回答1:


You are trying to pass there an enumeration field not a type identifier. You need to declare this enumeration separately (what is in the example below TGroup type).

Anyway there is an unwritten convention to use T prefix for each Type identifier so then you can easy recognize a Type. That's the reason why I renamed Wordset to TWordset. Also word is not a good name for fields or variables because it's also a data type in Delphi.

program Project1;

{$APPTYPE CONSOLE}

uses SysUtils, TypInfo;

type
  TGroup = (Flavour, Color, Place, Animal);

type
  TWordset = record
    Name: string;
    Group: TGroup;
  end;

var
  Str: string;
  Words: array [1..50] of TWordset;

begin
  Words[1].Name  := 'Vanilla';
  Words[1].Group := Flavour;
  Words[2].Name  := 'Green';
  Words[2].Group := Color;
  Words[3].Name  := 'Home';
  Words[3].Group := Place;
  Words[4].Name  := 'Cat';
  Words[4].Group := Animal;

  Str := GetEnumName(TypeInfo(TGroup), Integer(Words[3].Group));

  Writeln(Str);
  Readln;
end.


来源:https://stackoverflow.com/questions/6273290/getenumname-typeinfo-problems-pascal-delphi-console

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!