Bernard 18 Posted June 28, 2021 (edited) 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 June 28, 2021 by Bernard Stupidity Share this post Link to post
FPiette 383 Posted June 28, 2021 (edited) 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 June 28, 2021 by FPiette Share this post Link to post
David Heffernan 2345 Posted June 28, 2021 It's also pointless to use SetLength in this code because you then throw that array away and replace with a new one. 1 Share this post Link to post
Bernard 18 Posted June 29, 2021 I did not know that. Makes sense. Thanks Share this post Link to post