Solved

Collections question:

Posted on 2004-04-02
8
163 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
[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 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
Technology Partners: 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!

 
LVL 86

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 86

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

734 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