Solved

Collections question:

Posted on 2004-04-02
8
158 Views
Last Modified: 2010-05-02
if

    For Each item In SourceCollectnUnMatch
        colTemp2.Add item
    Next item

do something

then

 colTemp2.remove item


I get a type mismatch error???  Does it have to do with the key?  







0
Comment
Question by:brianpowell
8 Comments
 
LVL 4

Expert Comment

by:SSSoftware
ID: 10745109
You don't remove an item , You remove an Index of an item in the collextion or a Key of an item in the collection.

Ed
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10745465

colTemp2.remove 1  'reomves 1st item in collection
....


colTemp2.remove 66  'removes 66th item in the collection

...
0
 
LVL 1

Author Comment

by:brianpowell
ID: 10747602
interesting.  I have been doing  .remove item(i)

but why the type mismatch error?

if i use .add item, item

then .remove item

works well.

bp
0
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.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 10747652
You can remove items from a collection by index OR by key.  If the value passed in to the Remove method is a string, then the collection attempts to find the corresponding item that matches that key.  Since you didn't add anything to the colTemp2 collection with a key, you can only remove by index.

colTemp2.Add item ' Added WITHOUT a key
colTemp2.Add item, item ' Added WITH a key

Idle_Mind
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10748567
interesting.  I have been doing  .remove item(i)
but why the type mismatch error?
=================================
Because remove method of collection expects an Index which is an LONG wairable.
so you cannot  pass an object to it  or type mismatch happens.







0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10748620
sorry for the typos lol

wairable=variable
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10749497
As I said before, the Remove method does not expect only an index value; it can also take a string value.

I don't think you have a full understanding of how the Key concept in a collection works.  This small app demonstrates the use of key values in a collection.  This code adds, updates and removes items in the collection all based upon key values...which are strings.

Create a new project and add a ListBox, two TextBoxes, and two CommandButtons.  Paste the code below and hit run.  Select the different pets in the listbox to see their data and use the buttons to add, update or remove pet's and their data.

Regards,

Idle_Mind


' ---------------------------------------------------------------------
Option Explicit

Private petData As New Collection

Private Sub Form_Load()
    Dim pet As String, desc As String
   
    Command1.Caption = "Add/Update"
    Command2.Caption = "Remove"
    Text1.Text = ""
    Text2.Text = ""
   
    pet = "Dog"
    desc = "Furry and Loveable"
    petData.Add desc, pet
    List1.AddItem pet
   
    pet = "Cat"
    desc = "Furry but Mean"
    petData.Add desc, pet
    List1.AddItem pet
   
    pet = "Hamster"
    desc = "Furry but Squashable"
    petData.Add desc, pet
    List1.AddItem pet
   
    pet = "Fish"
    desc = "Slimey but Cute"
    petData.Add desc, pet
    List1.AddItem pet
End Sub

Private Sub List1_Click()
    ' if a pet has been selected...
    If List1.ListIndex <> -1 Then
        ' display the pet data in the textboxes
        Text1.Text = List1.List(List1.ListIndex)
        Text2.Text = petData.Item(List1.List(List1.ListIndex))
    End If
End Sub

Private Sub Command1_Click()
    On Error GoTo addPet
   
    Dim data As Variant
    Dim i As Integer
   
    ' if we have valid pet data...
    If Text1.Text <> "" And Text2.Text <> "" Then
        data = petData.Item(Text1.Text)
        ' update the existing pet data
        petData.Remove Text1.Text
        petData.Add Text2.Text, Text1.Text
    End If
    Exit Sub
   
addPet:
    ' add the new pet along with it's data
    petData.Add Text2.Text, Text1.Text
    List1.AddItem Text1.Text
    List1.ListIndex = List1.ListCount - 1
End Sub

Private Sub Command2_Click()
    Dim removeIndex As Integer
   
    ' get the currently selected pet index
    removeIndex = List1.ListIndex
    If removeIndex <> -1 Then ' if a pet is actually selected...
        ' then remove the selected pet
        petData.Remove List1.List(removeIndex)
        List1.RemoveItem (removeIndex)
       
        ' Select a new current pet (if any left)
        If removeIndex > List1.ListCount - 1 Then
            removeIndex = List1.ListCount - 1
        End If
        If List1.ListCount > 0 Then
            List1.ListIndex = removeIndex
        Else
            Text1.Text = ""
            Text2.Text = ""
        End If
    End If
End Sub
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

770 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