Need help with my logic for displaying only unique Topics in listbox

Hi. The code below takes a string of ID numbers, splits them at each comma, runs through a table of topics where the RefID of the table equals the RefID from the list of IDs in an array and lists them in Listbox3. There are some RefIDs that have the same Topic, and I would like to add to the listbox only the unique ones. My thought was to join all of the topic names into a string and then use IndexOf to scan the joinedTopics string. If the result is -1, that means it is not there and should be added, but the code below as is is not adding anything

I put a break in after the for i loop and see that the joinedTopics does in fact include all of the topic names as expected

If I change the last if…then from the main code below to this here (took out the IndexOf part), the listbox populates correctly but does have repeated topics, so I know my problem is in the IndexOf piece, but I cannot seem to get it to work right

if rsTopic.RowCount > 0 then
  Listbox3.AddRow(rsTopic.Column("Topic").StringValue)
end if

Here is the full code for the section

dim sql as String = "SELECT * FROM teachMonitor WHERE ID = '" + lbxDetail.Cell(lbxDetail.ListIndex, 6) + "'"
dim rs as RowSet = dbUser.SelectSQL(sql)

dim RefIDList as String = rs.Column("MonTopicCorr").StringValue

'this is used to check for repeaters and will only add if not present
dim joinedTopics as String

Var anArray() As String
anArray = RefIDList.Split(",")

for k as Integer = 1 to 4
  dim catTitle as String
  if k = 1 then
    catTitle = "Basic Nutrition"
  elseif k = 2 then
    catTitle = "Clinical"
  elseif k = 3 then
    catTitle = "Management"
  elseif k = 4 then
    catTitle = "Foodservice"
  end if
  
  'add domain heading and bold it
  Listbox3.AddRow(catTitle)
  Listbox3.CellBold(Listbox3.LastAddedRowIndex, 0) = true
  
  'run loop
  for i as integer = 0 to anArray.Ubound
    dim sqlTopic as String = "SELECT * FROM MonitorExamTopics WHERE RefID = '" + anArray(i) + "' AND DomainNumber = 'Domain " + k.ToText + "'"
    dim rsTopic as RowSet = dbUser.SelectSQL(sqlTopic)
    
    joinedTopics = joinedTopics + rsTopic.Column("Topic").StringValue
    
    if rsTopic.RowCount > 0 and joinedTopics.IndexOf(0, rsTopic.Column("Topic").StringValue) = -1 then
      Listbox3.AddRow(rsTopic.Column("Topic").StringValue)
    end if
  next
  
  'add blank space
  Listbox3.AddRow("")
next

I got it. I had the joinTopics in the wrong spot in the loop. Had to put it after the if…then. Prior to, this was adding the topic name, not returning -1 since it was already in the string. After it is added once, then add it to the joinTopics string, then on the next loop, see if it’s there. Sorry for posting to prematurely!

'run loop
for i as integer = 0 to anArray.Ubound
  dim sqlTopic as String = "SELECT * FROM MonitorExamTopics WHERE RefID = '" + anArray(i) + "' AND DomainNumber = 'Domain " + k.ToText + "'"
  dim rsTopic as RowSet = dbUser.SelectSQL(sqlTopic)
  
  if rsTopic.RowCount > 0 and joinedTopics.IndexOf(0, rsTopic.Column("Topic").StringValue) = -1 then
    Listbox3.AddRow(rsTopic.Column("Topic").StringValue)
  end if
  
  joinedTopics = joinedTopics + rsTopic.Column("Topic").StringValue
next

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.