转换一个变量数组到一个动态数组 [英] Converting a Variant Array to a Dynamic Array

查看:215
本文介绍了转换一个变量数组到一个动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换一个变量数组(双打,但它可以是任何东西我猜的)来动态数组。我通常使用的DynArrayFromVariant和DynArrayToVariant程序,但在这种情况下,我的变种数组是基于1。这两个函数似乎只在0基于阵列的工作。任何想法,我怎么可能做我需要做什么?

I'm trying to convert a variant array (of doubles, but it could be anything I guess) to a dynamic array. I usually use the DynArrayFromVariant and DynArrayToVariant procedures, but in this case my variant arrays are 1 based. These two functions only seem to work on 0 based arrays. Any idea how I could do what I need to do?

推荐答案

如果你知道你的数组元素的类型,你可以写更高效(虽然不太通用)code:

If you know the type of your array elements you can write more efficient (while less generic) code:

function DoubleDynArrayFromVarArray(const V: Variant): TDoubleDynArray;
var
  P: Pointer;
  Count: Integer;
begin
  Result := nil;
  if not VarIsArray(V) or (VarType(V) and varTypeMask <> varDouble) or
    (VarArrayDimCount(V) <> 1) then
    raise EVariantInvalidArgError.Create(SVarInvalid);

  Count := VarArrayHighBound(V, 1) - VarArrayLowBound(V, 1) + 1;
  if Count = 0 then
    Exit;

  P := VarArrayLock(V);
  try
    SetLength(Result, Count);
    Move(P^, Result[0], Count * SizeOf(Double));
  finally
    VarArrayUnlock(V);
  end;
end;

这篇关于转换一个变量数组到一个动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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