Solved

stringlist

Posted on 2001-07-18
8
331 Views
Last Modified: 2012-05-04
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?

0
Comment
Question by:mantra246
8 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 150 total points
ID: 6293207
Try something like this:

If sl.IndexOf(thestring) = -1 then
  sl.Add(thestring)
else
  ShowMessage(thestring + ' is already in the list');
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6293213
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6293251
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 9

Expert Comment

by:ITugay
ID: 6293296
meikl :-)

yes, it doesn't work if list is not sorted.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6293315
hi, igor,

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

meikl ;-)
0
 
LVL 17

Expert Comment

by:geobul
ID: 6293407
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
 
LVL 1

Expert Comment

by:Oli2
ID: 6293759
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
 

Author Comment

by:mantra246
ID: 6296121
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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to manage invalidate between two tvirtualstringtree in same form? 1 117
Delphi XE10 Round Image 2 129
Making delphi communicate with a c# service 16 104
CheckListBox usage 3 59
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question