GetEnumName TypeInfo问题(pascal / Delphi控制台) [英] GetEnumName TypeInfo problems (pascal / Delphi console)

查看:251
本文介绍了GetEnumName TypeInfo问题(pascal / Delphi控制台)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Delphi 7在控制台应用程序上工作,遇到了问题。在

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),

错误显示为 [Error] Project1.dpr(26):TYPEINFO标准函数需要类型标识符,如果有人可以提供帮助,这将是一个很大的帮助!

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!

干杯!

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.


推荐答案

您正在尝试向其中传递枚举字段,而不是类型标识符。您需要单独声明此枚举( TGroup 类型下面的示例中的内容)。

无论如何,对于每个枚举都使用 T 前缀的不成文约定类型标识符,因此您可以轻松识别类型。这就是为什么我将 Wordset 重命名为 TWordset 的原因。另外 word 也不是字段或变量的好名字,因为它也是Delphi中的数据类型。

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.

这篇关于GetEnumName TypeInfo问题(pascal / Delphi控制台)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆