Jump to content

getting values out of arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
kruegs35

kruegs35

    Newbie

  • Members
  • Pip
  • 8 posts
I am having some trouble with arrays. I want to set a variable equal to a value in an array as it goes through a loop. The array and the variable are declared as integers. When I check the value of the variable while stepping through the code, the variable is listed as unaccessible. However, if I declare the variable as a string and do an IntToStr with the array value, I can get a value in the variable. Any reason for this? Is there a way to pull the values from an array as integers?

#2
ooisootuck

ooisootuck

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
Hi,

Can you please post your code?

Thanks

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Your variable will have to be of the same data type as the array's elements. Without seeing your code, it's hard to see what may be going on.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
kruegs35

kruegs35

    Newbie

  • Members
  • Pip
  • 8 posts
I do not have the actual code with me, but I will try to give the basics.


var

  i: integer;

  x: integer;

  MyArray: array[0..5] of integer;

begin

  for i = 0 to 5 do begin

    x := MyArray[i];

end; 


When I step through the code and follow the local variables, the value for x would say "unaccessible due to optimization". However, if I declare x as a string and type x:= IntToStr(MyArray[i]), I will get the value out of the array as a string.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Try this:
var
  i: integer;
  x: integer;
  MyArray: array[0..5] of integer;
begin
  for i = 0 to 5 do begin
  begin
    x := MyArray[i];
    x := x;
  end;
end;

Delphi's debugger won't let you see an uninitialized variable, and since the for loop just kinda sits there, you never get a chance to see the value of x. You should be inspecting the value of x on the "x := x;" line.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog