List index out of bounds error

I'm trying to use the following code to parse through a TStringList and check each string to see if it contains a particular substring.  If it does, then I want that string removed from the TStringList.

Count := AddList.Count;
for X := 0 to Count - 1 do
  if Pos(HomeDrive, Addlist.Strings[X]) <> 0 then

When it hits the last element in the TStringList, I get the "List index out of bounds (X)" (where X is the last string in the list).  It doesn't matter how many strings are present, or whether or not any or all of them are to be removed.  If I just tell it to ShowMessage and not delete the strings as it parses the list it works fine, but when I instruct it to delete the string it then blows up.

I'm stuck.  Any help would be appreciated.
Who is Participating?
kretzschmarConnect With a Mentor Commented:
hi orca,

just replace your code with this little change

Count := AddList.Count;
for X := Count - 1 downto 0 do
  if Pos(HomeDrive, Addlist.Strings[X]) <> 0 then

meikl ;-)
OrcaAuthor Commented:
Thanks alot meikl for your prompt response -- that was all it took.

This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.
Thank you,
Moderator @ Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.