Строковое представление типа контента Варианта?

Это не граница. Это фокус. Вы можете удалить его, используя:

jButton1.setFocusPainted(false);
6
задан w5m 13 November 2014 в 20:12
поделиться

4 ответа

Просто используйте встроенную функцию Delphi для получения строкового представления типа Variant.

var
  MyVariantType: string;
  MyVariant: Variant;
begin
  MyVariant := 'Hello World';
  MyVariantType := VarTypeAsText(VarType(MyVariant));
  ShowMessage(MyVariantType); //displays: String

  MyVariant := 2;
  MyVariantType := VarTypeAsText(VarType(MyVariant));
  ShowMessage(MyVariantType); //displays: Byte
end;
12
ответ дан 8 December 2019 в 14:46
поделиться

Quoting from the Delphi 2007 help:

Use GetEnumName to convert a Delphi enumerated value into the symbolic name that represents it in code.

That means that you can't use it for that purpose, as TVarData.VType is not an enumerated value, but an integer which is set to one of the constants in System.pas that are taken from the Windows SDK wtypes.h file. Look at the source of GetEnumName(), it does immediately return a string containing the value of the integer.

Edit:

is there any other way to get the string representation of TVarData.VType

You can determine the string representation manually. First you need to be aware of that there are several bits of information encoded in that integer, so a simple case statement or array lookup will not work. The lower 12 bits are the type mask, and the upper bits encode information about whether it is a vector or array type and whether it is given by reference or not. The important parts are:

const
  varTypeMask = $0FFF;
  varArray    = $2000;
  varByRef    = $4000;

So you could do something like:

function VariantTypeName(const AValue: TVarData): string;
begin
  case AValue.VType and varTypeMask of
    vtInteger: Result := 'integer';
    // ...
  end;

  if AValue.VType and varArray <> 0 then
    Result := 'array of ' + Result;
  if AValue.VType and varByRef <> 0 then
    Result := Result + ' by ref';
end;
3
ответ дан 8 December 2019 в 14:46
поделиться

Поскольку это не перечисление, вам придется сделать это вручную. Напишите что-нибудь вроде этого:

function VariantTypeName(const value: TVarData): string;
begin
  case value.VType of
    vtInteger: result := 'integer';
    //and so on
end;

Или, поскольку значения в System.pas перечислены по порядку, вы можете попробовать объявить константный массив строк и заставить вашу функцию VariantTypeName вернуть соответствующий член массива.

1
ответ дан 8 December 2019 в 14:46
поделиться

Вот мысль для Delphi Versions, которые не поддерживают VARTYPESTEXT: вы можете определить перечисляемый тип самостоятельно, который следует за значениями VTYPE:

type
  {$TYPEINFO ON}
  TMyVarType = (
    varEmpty = System.varEmpty, 
    varNull = System.varNull,
    // etc...
    );

(наполнить неиспользуемые слоты enum - см. Почему я получаю «Тип не имеет ошибки yumpinfo» с типом enum для рассуждения позади этого).

Далее используйте эти функции для чтения типа вариантов в качестве собственного перечисления типа:

function MyVarType(VType: TVarType): TMyVarType; overload;
begin
  Result := TMyVarType(VType);
end;

function MyVarType(V: Variant): TMyVarType; overload;
begin
  Result := TMyVarType(TVarData(V).VType);
end;

, а затем вы можете преобразовать его в строку, как это:

function VarTypeToString(aValue: TMyVarType): string;
begin
  Result := GetEnumName(TypeInfo(TMyVarType), Ord(aValue));
end;
0
ответ дан 8 December 2019 в 14:46
поделиться
Другие вопросы по тегам:

Похожие вопросы: