Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array Checking

Posted on 2002-05-09
8
Medium Priority
?
197 Views
Last Modified: 2010-05-02
I have two separate arrays. One is called strAddArray and the other is called strRemoveArray. What I am trying to do is select an item from a listbox. and add or remove it into another listbox. Evertime I add it it goes into strAddArray and evertyime I remove an item it goes into strRemoveArray.  However, if I add and remove the same several times I create multiple copies of the same item within both arrays. Before I can update the DB I need to have the arrays setup correctly.  Below is what I have thus far. The problem that I am running into is if I have a duplicate I remove it completely and have no copy of it all together.  This is not as easy as it looks for me because there are so many scenarios, and I do not want anything to slip through the cracks.

Thanks,
Roger.
====================================

'Checks items added

If intRemove > 0 Then
    For x = 0 To 500
      For y = (x + 1) To 500
      If strRemoveArray(x) = strRemoveArray(y) And strRemoveArray(x) <> "" Then
         strRemoveArray(y) = Empty
         intRemove = intRemove - 1
      End If
     Next y
    Next x
   End If


'Checks items removed

If intAdd > 0 Then
    For x1 = 0 To 500
      For y1 = (x1 + 1) To 500
      If strAddArray(x1) = strAddArray(y1) And strAddArray(x1) <> "" Then
         strAddArray(x1) = Empty
         intAdd = intAdd - 1
      End If
     Next y1
    Next x1
   End If

'Compares the two above arrays for any lingering duplicates

If intAdd > 0 And intRemove > 0 Then
    For x2 = 0 To 500
      For y2 = 0 To 500
      If strAddArray(x2) = strRemoveArray(y2) And strAddArray(x2) <> "" Then
         strAddArray(x2) = Empty
         intAdd = intAdd - 1
            If strRemoveArray(y2) = strAddArray(x2) And strRemoveArray(y2) <> "" Then
               strRemoveArray(y2) = Empty
               intRemove = intRemove - 1
            End If
      End If
     Next y2
    Next x2
   End If
0
Comment
Question by:RogerH1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 300 total points
ID: 6999378
As an alternative you may want to use Collections instead of arrays.  They have built in methods for adding and removing.

Just a thought,
Anthony
0
 

Author Comment

by:RogerH1
ID: 6999388
I am not familiar with what you mean by "Collections". Would you explain to me please.

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6999530
Please maintain your open questions:
Rollback will not work Date: 02/07/2002 12:17PM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20264417
Table locking problems Date: 02/11/2002 10:57AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20265663
Lock out a user Date: 02/26/2002 10:03AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20271250
Window placement when minimized Date: 03/07/2002 07:17AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20274441
Unable to pull information properly into report Date: 02/22/2002 02:52PM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20270066
Unable to reprint the first page Date: 03/08/2002 07:21AM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20274932

Thanks,
Anthony
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:HobbitHouse
ID: 7000165
you could just use binary trees instead of arrays, then when you stuff duplicates you can just ignore them and each tree will maintain exactly one of each of the things you put in it, so when you unwind it into the db, you have no duplicates
0
 
LVL 2

Expert Comment

by:ventond
ID: 7000381
Why have arrays at all? The list boxes are working like arrays already.

After you are ready to update your database:

For ndx = 0 to addlist.listcount-1
 ' do add processing for addlist.list(ndx)
next

for ndx = 0 to removelist.listcount-1
 ' do remove processing for removelist.list(ndx)
next
0
 

Author Comment

by:RogerH1
ID: 7001288
These are all great ideas, but please understand something that I did not explain. That is, when I open this window it reads in from the DB and then the user can add and remove from their initial choices.

Unless I have made this more complicated than it needs to be, I can ONLY see it being done with some sort of array.  Since yesterday I have started working on understanding collections and worked up some code that is absolutely great. It checks current collection for duplicates and if they exists it never gets entered.  It is far less code than using an array, and seems to be easier to work with for what I am doing.

Now on the other hand, if someone feels that I am still making this harder than it needs to be please let me know.  I will leave this open until Monday at which time I will go ahead and close it if no one has a better idea.

Thanks,
Roger.
0
 

Author Comment

by:RogerH1
ID: 7006019
I looked into using the Collections class. It took me a while to find something good on the subject, but once I did I tried it in a sample program that I created. I feel that it works much easier than using arrays, and in less the code.  

I wanted to give you an "A", but I your answer was only a direction to try, and no explanation or sample code came with it.  However, thank you for telling me about it. I learned something new.

Below is the sample program that I put together in case there are others that are not familiar with it like I was. It is nothing fancy, just enough to get a basic feel for how it works.

The Add button adds item typed in into a listbox, but also updates the Collections. The Show button displays what is in the Collections itself. Highlighting one of the items in the listbox showing the Collections will remove it from the Collections.

Roger.

===========================================
Private Sub cmdClear_Click()
   lstShow.Clear
   lstList.Clear
   txtItem.Text = ""
   Label1.Caption = ""
   num = 0
   Debug.Print test.Count
For x = 1 To test.Count
   test.Remove 1
Next x
   cmdShow.Enabled = False
   cmdClear.Enabled = False
    txtItem.SetFocus
End Sub

Private Sub cmdExit_Click()
   End
End Sub

Private Sub cmdRemove_Click()
Dim x As Integer
Dim y As Integer
Dim str As String

str = lstShow.Text
 test.Add " "
For x = 1 To test.Count - 1
'   Debug.Print test.Item(x)
      If test.Item(x) = str Then
         test.Remove (x)
      End If
   lstShow.Clear
Next x
End Sub

Private Sub cmdShow_Click()
For x = 1 To test.Count
   lstShow.AddItem test.Item(x)
Next x
End Sub

Private Sub cmdAdd_Click()
Dim str As String

str = Trim(txtItem.Text)

' Do not add a blank field
If str = "" Then
   txtItem.Text = ""
   txtItem.SetFocus
   Exit Sub
End If
num = num + 1

'Checks for duplicates within the collection and does not add it
For x = 1 To test.Count
   If test.Item(x) = str Then
      MsgBox "Match"
'      num = num - 1
      txtItem.Text = ""
       txtItem.SetFocus
      Exit Sub
   End If
Next x
      lstList.AddItem txtItem.Text
      Label1.Caption = num
      test.Add str
      txtItem.Text = ""
      txtItem.SetFocus
      cmdShow.Enabled = True
      cmdClear.Enabled = True

End Sub
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7009896
>> I wanted to give you an "A", but I your answer was only a direction to try, and no explanation or sample
code came with it.  However, thank you for telling me about it. I learned something new.<<

The reason I did not elaborate beyond stating that it could be resolved with Collections was because I have no interest in suggesting solutions to questioners that do not abide by the EE Guidelines (http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp) and leave questions open indefinitely.

In any case, thank-you for the points and for the opportunity of explaining my position.

Anthony
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

610 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