gpt4 book ai didi

delphi - 动态数组变量未显示在局部变量调试窗口中

转载 作者:行者123 更新时间:2023-12-03 18:22:05 26 4
gpt4 key购买 nike

我有一些我以前从未见过的东西。调试时不会出现在“局部变量”窗口中的局部变量。我正在附上一张图片。 WHY U NO SHOWN THERE?

如您所见,我在代码中使用了该变量,并且在调试时禁用了优化。但我也使用泛型和匿名方法,我不是专家。 有问题的过程的代码是这样的(该过程的作用是按第一个数组中的字符串长度按降序排列前两个数组 ArrayNomes、ArrayValores):

  procedure OrdenarArrays(var ArrayNomes, ArrayValores: array of string; var ArrayIndices: array of Integer);
var
Comparer: IComparer<Integer>;
I: Integer;
tmpNomesCampos, tmpValoresCampos: array of String;
begin

SetLength(tmpNomesCampos, cdsCadastro.FieldCount);
SetLength(tmpValoresCampos, cdsCadastro.FieldCount);

//Carregar os NomesCampos para serem usados na comparação
for I := 0 to High(arrayIndices) do
begin
tmpNomesCampos[I] := ArrayNomes[I];
end;

{ Cria novo delegatedcomparer. Ele permite o uso de um callback para comparar os arrays}
Comparer := TDelegatedComparer<Integer>.Create(
{ TComparison<Integer> }
function(const Left, Right: Integer): Integer
begin
{colocar em ordem decrescente de acordo com o tamanho do nome do campo}
// Result := Left - Right;
Result := -(Length(tmpNomesCampos[Left]) - Length(tmpNomesCampos[Right]));
end);

{ Ordena o Array base }
TArray.Sort<Integer>(arrayIndices, Comparer);

//Reordenar os NomesCampos de acordo com o array IndicesCampos
for I := 0 to High(arrayIndices) do
begin
tmpNomesCampos[I] := ArrayNomes[arrayIndices[I]];
tmpValoresCampos[I] := ArrayValores[arrayIndices[I]];
end;

//Salvar nos arrays definitivos;
for I := 0 to High(arrayIndices) do
begin
ArrayNomes[I] := tmpNomesCampos[I];
ArrayValores[I] := tmpValoresCampos[I];
end;

end;

这个变量没有显示错误吗?这是一个已知的错误吗?或者它可能是使用我不知道的泛型和匿名方法的功能?

系统:Windows 7 64位/Delphi XE(最新更新)


更新:将代码更改为下面的简化控制台版本。这可能会帮助任何想要在 Delphi 版本中进行测试的人。注意:它不会填充原始数组,因为没有必要显示问题;

program Project1;


{$APPTYPE CONSOLE}

uses
SysUtils, Generics.Collections, Generics.Defaults;

procedure OrdenarArrays(var ArrayNomes, ArrayValores: array of string; var ArrayIndices: array of Integer);
var
Comparer: IComparer<Integer>;
I: Integer;
tmpNomesCampos, tmpValoresCampos: array of String;
begin

SetLength(tmpNomesCampos, Length(arrayIndices));
SetLength(tmpValoresCampos, Length(arrayIndices));

//Carregar os NomesCampos para serem usados na comparação
for I := 0 to High(arrayIndices) do
begin
tmpNomesCampos[I] := ArrayNomes[I];
end;

{ Cria novo delegatedcomparer. Ele permite o uso de um callback para comparar os arrays}
Comparer := TDelegatedComparer<Integer>.Create(
{ TComparison<Integer> }
function(const Left, Right: Integer): Integer
begin
{colocar em ordem decrescente de acordo com o tamanho do nome do campo}
// Result := Left - Right;
Result := -(Length(tmpNomesCampos[Left]) - Length(tmpNomesCampos[Right]));
end);

{ Ordena o Array base }
TArray.Sort<Integer>(arrayIndices, Comparer);

//Reordenar os NomesCampos de acordo com o array IndicesCampos
for I := 0 to High(arrayIndices) do
begin
tmpNomesCampos[I] := ArrayNomes[arrayIndices[I]];
tmpValoresCampos[I] := ArrayValores[arrayIndices[I]];
end;

//Salvar nos arrays definitivos;
for I := 0 to High(arrayIndices) do
begin
ArrayNomes[I] := tmpNomesCampos[I];
ArrayValores[I] := tmpValoresCampos[I];
end;

end;

var
NomesCampos, ValoresCampos: array of String;
IndicesCampos: array of Integer;
I: Integer;
begin
try

SetLength(NomesCampos, 42);
SetLength(ValoresCampos, 42);
SetLength(IndicesCampos, 42);

// for I := 0 to 41 do

OrdenarArrays(NomesCampos, ValoresCampos, IndicesCampos);

Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

最佳答案

这是 XE5 中仍然存在的调试器/IDE 错误。正如您所怀疑的,问题与匿名方法的使用有关。关键是变量是否被捕获。捕获变量后,该变量不会显示在“局部变量”窗口中。这是我能编造的最短的 SSCCE:

{$APPTYPE CONSOLE}

uses
System.SysUtils;

procedure Foo1;
var
func: TFunc<Integer>;
val: Integer;
begin
val := 666;
func :=
function: Integer
begin
Result := val;
end;
end;

procedure Foo2;
var
func: TFunc<Integer>;
val: Integer;
begin
val := 666;
func :=
function: Integer
begin
Result := 666;
end;
end;

begin
Foo1;
Foo2;
Readln;
end.

在调试器中,Foo1.val 不会显示在“局部变量”窗口中。但 Foo2.val 确实如此。

QC#121821

关于delphi - 动态数组变量未显示在局部变量调试窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21163100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com