stringlist

I am trying to ensure that a list of string i am dealing with are unique.  I decided to use a stringlist as i know it is supposed to handle duplicates but can't get my idea to work.

Does anyone know how i can use the stringlist so that if the text i am about to add is already in the stringlist it will not add it and will notify me that the string is a duplicate without causing any errors through windows, etc?

mantra246Asked:
Who is Participating?
 
EpsylonConnect With a Mentor Commented:
Try something like this:

If sl.IndexOf(thestring) = -1 then
  sl.Add(thestring)
else
  ShowMessage(thestring + ' is already in the list');
0
 
jswebyCommented:
That is exactly it; if the method property returns a value of more than -1 then the item already exists in the stringlist.

J.
0
 
kretzschmarCommented:
there is another method also,
but didn't have it in mind correctly

you can set the sorted property to true,
and set the duplcate bahaviour to this what you need like
dupIgnore, dupAccept, dupAbort,..

just set this before you add the items

sorry, have not the correct property-names in mind

meikl ;-)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
ITugayCommented:
meikl :-)

yes, it doesn't work if list is not sorted.
0
 
kretzschmarCommented:
hi, igor,

its really hard to give
advice just from head :-(

meikl ;-)
0
 
geobulCommented:
Hi,
From Delphi help:

TStringList.Duplicates

Specifies whether duplicate strings can be added to sorted lists.

type TDuplicates = (dupIgnore, dupAccept, dupError);
property Duplicates: TDuplicates;

Description

Set Duplicates to specify what should happen when an attempt is made to add a duplicate string to a sorted list. The value of Duplicates should be one of the following.

Value     Meaning

dupIgnore     Ignore attempts to add duplicate strings to the sorted list.
dupError     Raise an EStringListError exception when an attempt is made to add duplicate strings to the sorted list.
dupAccept     Permit duplicate strings in the sorted list.

Set Duplicates before adding any strings to the list. Setting Duplicates to dupIgnore or dupError does nothing about duplicate strings that are already in the list.

Note:     Duplicates does nothing if the list is not sorted.

Regards, Geo
0
 
Oli2Commented:
procedure AddToStrings(s: String var sl: TStringList);
begin
  if not sl.Sorted then
     sl.Sorted := True;
  if not sl.Duplicates = duError then
     sl.Duplicates := dupError;
  try
    sl.Append(s);
  except
    ShowMessage('This string is already in the list');
  end;
end;
//---------------------------------------------------------
Hope this helps.
I typed it here, so no checking, but it should work and give you an idea.

Cheers,
Oli
0
 
mantra246Author Commented:
epsylon:  ouch!  when i saw the code you gave it really hurt because it was so simple and i never thought of it till i saw it.

everyone:  Thanks so much for your input it was really appreciated - as is the time and effort you guys put in.
0
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.