Jump to content
Bernard

I have and Array of Word issue. Help me understand.

Recommended Posts

I came accross an issue when assigning values to an 'array of word' earlier

 

This works 

var
  WordArray : TArray<Word>;
begin
  setlength(WordArray, 10);
  WordArray := [0, 7323, 0, 7224, 0, 7124, 0, 7024, 0, 7004];
end;

 

This works 

var
  WordArray : TArray<Word>;
begin
  setlength(WordArray, 10);

  WordArray := [0, word(73232), 0, 7224, 0, 7124, 0, 7024, 0, 7004];

end;

 

This does not work

var
  WordArray : TArray<Word>;
begin
  setlength(WordArray, 10);

  WordArray := [0, 73232, 0 ,7224, 0, 7124, 0, 7024, 0, 7004];

end;

 

[dcc32 Error] TestArray.pas(28): E1012 Constant expression violates subrange bounds

 

Can someone explain why the above code is not accepted.  For curious reasons.

 

I am stupid I see why I was outside the max range of Word.

 

 

Edited by Bernard
Stupidity

Share this post


Link to post

You have an array of word. Each array cell has to be a word, that is a 16 bit unsigned integer (0..65535). When you try to assign 73232, you are outside of the range 0..65535 giving the error you see. When you assign word(73232), you ask the compiler to convert 72232 to a word (will simply be truncated to the lower 16 bits) and it works.

 

Edited by FPiette

Share this post


Link to post

It's also pointless to use SetLength in this code because you then throw that array away and replace with a new one. 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×