[fpc-pascal] Get highest element of a StringList

James Richters james.richters at productionautomation.net
Sun Sep 11 22:03:18 CEST 2022


The loops is just an example because in my original post of why I wanted to
get the highest element of a stringlist was because I wanted a for loop that
wasn't as clumsy as

For I:=0 to myStringlist.count-1 Do

This came up because thanks to this discussion list, I learned I could use
High() and Low() for my dynamic arrays,  so I went through replacing all
instances of 

Length(MyDynamicArray)-1 with  High(MyDynamicArray)

So my for loops went from:

For I:=0 to Length(MyDynamicArray)-1 Do 
To 
For I:=0 to High(MyDynamicArray) Do

Which I find much more clear and less prone to problems.. I have forgot the
-1 before and it gives range check errors of course.. so I'm switching
everything to use High() instead and will just automatically use High() in
the future, thus preventing the issue all together.

I'm really happy with the helper function and using MyStringList.High.  I
think .High should just be added to all things that have a .Count because
everyone always needs it ALL THE TIME.  But I am happy it's possible to add
a helper function to add it myself.

James

>Why the loop?



More information about the fpc-pascal mailing list