none
IndexOf' is not a member of 'Microsoft.VisualBasic.Collection'. (BC30456) RRS feed

  • Question

  • I'm having trouble with a more complex program, so I've created a simple program to highlight my issue (see code block below).  When I declare a Collection generically (like I have below) and try to call IndexOf, I get the following compiler error:

    'IndexOf' is not a member of 'Microsoft.VisualBasic.Collection'. (BC30456)

    If I instead declare my Collection as a collection of strings (Public mycollection As New Collection(Of String)), I then have access to the usual methods, but the .Add method then only allows a single string to be passed to it.  This means that I can't use a key, which seems to be the point of a collection.  Is there any way to have a general, keyed collection that can use IndexOf?

    Imports System
    Imports System.Collections
    Imports System.Collections.Generic
    Imports System.Collections.ObjectModel
    
    Public Partial Class MainForm
    	
    	Public mycollection As New Collection()
    	
    	Public Sub New()
    		Me.InitializeComponent()
    	End Sub
    
    	Sub MainFormLoad(sender As Object, e As EventArgs)
    		mycollection.Add("one", "one")
    		MessageBox.Show(mycollection.IndexOf("one").ToString())
    		'The above line returns the compiler error: 
    		''IndexOf' is not a member of 'Microsoft.VisualBasic.Collection'. (BC30456)
    	End Sub
    	
    End Class
    

    Sunday, May 20, 2012 8:39 PM

Answers

  • Why do you need the index of the item? You can get the value associated with the key, and you can check that the key exists before attemtpting to get it. Is that sufficient? The index of the item provides no useful information for a dictionary. If you absolutely must have the index, then a dictionary probably isn't appropriate. But teh point of a dictionary is to eliminate the need to know where in the collection any particular item is stored - it is sufficient to be able to retrieve it as required.
    Sunday, May 20, 2012 10:34 PM
  • I didn`t say that there is a IndexOf() method. To access the structure you only need the key. Like i have shown with the example.

    editableStructure is the "test" structure which is assigned to the key "theKey".

    To test if a key exists you can use the ContainsKey("theKey") method.

    So i really don`t understand whatfor you need the IndexOf() method.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Sunday, May 20, 2012 10:38 PM
  • Nicholas, 

    Try to avoid that afterbirth of VB6, this one was created in the true Microsoft VisualBasic namespace in version 7.x but misses everything what other collections has and also works completely different from other collections. Hit has not any relation to a List, Collection or whatever what is in .Net like that.

    You will seen that if you use it you will sooner or later be in trouble. 

    In your case I would use a generic dictionary

    Thousand ways to implement one of them:

    Imports System
    Imports System.Collections
    Imports System.Collections.Generic
    Imports System.Collections.ObjectModel
    Partial Public Class MainForm
        Public mycollection As New Dictionary(Of String, String)
        Public Sub New()
            Me.InitializeComponent()
        End Sub
        Sub MainFormLoad(sender As Object, e As EventArgs) Handles MyBase.Load
            mycollection.Add("one", "two")
            Dim result As String = ""
            mycollection.TryGetValue("one", result)
            MessageBox.Show(result)
        End Sub
    End Class


    Success
    Cor


    Monday, May 21, 2012 5:07 AM
  • I tried using your dictionary example, but I still can't access the IndexOf method.

    Here's the compiler error:

    'IndexOf' is not a member of 'System.Collections.Generic.Dictionary(Of String, List_vs_Collection.test)'. (BC30456)

    ...




    Do you need both a 'key' and an 'index'?

    A Dictionary will allow you to access a collection by key. It is, by it's nature, unsorted, so getting an index is meaningless.

    If you need both, then you'll need to create a custom collection of some kind. If you just need an Index, then you can use a List() and create your own comparator (I'm not sure exactly how to do this, off the top of my head).

    If you have Classes instead of Structures, you can wrapper both the dictionary and a list in a custom class which gives you both an index and a keyed lookup. This can be very efficient, and gives you full control over the functionality.


    Stephen J Whiteley

    Monday, May 21, 2012 1:38 PM
    Moderator

All replies

  • Nicholas,

    what exactly do you want to achieve with the IndexOf() method ( what for do you need it ) ?


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Sunday, May 20, 2012 8:56 PM
  • The actual application is a bit convoluted.  It's a collection of a structure.  I need the IndexOf method when I need to update the structure at a given location in the collection.  This gives me the index of the item so that I can Remove and then Insert that item, thus updating the structure at the correct location in the collection.

    Sunday, May 20, 2012 9:15 PM
  • So is the "one" inside this statement:

    mycollection.Add("one", "one")

    a unique value ( it doesn`t occur twice ) ?


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Sunday, May 20, 2012 9:24 PM
  • Keep in mind that this is just a silly example to highlight the issue.  In this example, "one" is both the key and the element in the collection.  In my real code, the key is different for every element in the collection, and the elements are structures.
    Sunday, May 20, 2012 9:38 PM
  • I just looked at the first value of you example.

    So the first parameter is the key which is unique.

    For this you can use a Dictionary(Of TKey,TValue) like:

        Private Structure test
            Dim value As Integer
            Dim value2 As String
        End Structure
            Dim myCollection As New Dictionary(Of String, test)
            myCollection.Add("theKey", New test())
            Dim editableStructure As test = myCollection.Item("theKey")
            editableStructure.value2 = "lala"


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Sunday, May 20, 2012 9:46 PM
  • In my real code, the key is different for every element in the collection, and the elements are structures.

    In that case you can use a dictionary of structures, and access each item in the dictionary using the key. There should be no need to do an index of because you are not interested in where the dictionary items are stored in the collection - you simply need to be able to access them by key.
    http://msdn.microsoft.com/en-us/library/xfhwa508

    Sunday, May 20, 2012 9:53 PM
  • Sure... I could solve the issue in the example by using a dictionary instead of a generic collection.  However, that doesn't help my actual issue.  I need a way to have a collection of structures associated to string keys.  This same collection also needs to be able to use the methods IndexOf, Contains, Remove, Insert, etc.  Unless I'm mistaken, Dictionary only allows for collections of strings, not structures.
    Sunday, May 20, 2012 9:58 PM
  • Sure... I could solve the issue in the example by using a dictionary instead of a generic collection.  However, that doesn't help my actual issue.  I need a way to have a collection of structures associated to string keys.  This same collection also needs to be able to use the methods IndexOf, Contains, Remove, Insert, etc.  Unless I'm mistaken, Dictionary only allows for collections of strings, not structures.

    In the example above Dictionary's key is a string and the Value is a structure. And also as Acamar has written, to access the structure you only need the key ( which is a also shown in the sample ).

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Sunday, May 20, 2012 10:03 PM
  • Woops!  Somehow I missed that.  Sorry, guys, and thanks!

    Sunday, May 20, 2012 10:07 PM
  • I tried using your dictionary example, but I still can't access the IndexOf method.

    Here's the compiler error:

    'IndexOf' is not a member of 'System.Collections.Generic.Dictionary(Of String, List_vs_Collection.test)'. (BC30456)

    And here's the code I ran:

    Public Structure test

    Dim value As Integer

    Dim value2 As String End Structure Public Partial Class MainForm Public Sub New() Me.InitializeComponent() End Sub Sub MainFormLoad(sender As Object, e As EventArgs) Dim myCollection As New Dictionary(Of String, test) myCollection.Add("theKey", New test()) Dim editableStructure As test = myCollection.Item("theKey") editableStructure.value2 = "lala" Dim i As Integer = myCollection.IndexOf("theKey") 'The above line generates a compiler error. End Sub End Class





    Sunday, May 20, 2012 10:30 PM
  • Why do you need the index of the item? You can get the value associated with the key, and you can check that the key exists before attemtpting to get it. Is that sufficient? The index of the item provides no useful information for a dictionary. If you absolutely must have the index, then a dictionary probably isn't appropriate. But teh point of a dictionary is to eliminate the need to know where in the collection any particular item is stored - it is sufficient to be able to retrieve it as required.
    Sunday, May 20, 2012 10:34 PM
  • I didn`t say that there is a IndexOf() method. To access the structure you only need the key. Like i have shown with the example.

    editableStructure is the "test" structure which is assigned to the key "theKey".

    To test if a key exists you can use the ContainsKey("theKey") method.

    So i really don`t understand whatfor you need the IndexOf() method.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Sunday, May 20, 2012 10:38 PM
  • Nicholas, 

    Try to avoid that afterbirth of VB6, this one was created in the true Microsoft VisualBasic namespace in version 7.x but misses everything what other collections has and also works completely different from other collections. Hit has not any relation to a List, Collection or whatever what is in .Net like that.

    You will seen that if you use it you will sooner or later be in trouble. 

    In your case I would use a generic dictionary

    Thousand ways to implement one of them:

    Imports System
    Imports System.Collections
    Imports System.Collections.Generic
    Imports System.Collections.ObjectModel
    Partial Public Class MainForm
        Public mycollection As New Dictionary(Of String, String)
        Public Sub New()
            Me.InitializeComponent()
        End Sub
        Sub MainFormLoad(sender As Object, e As EventArgs) Handles MyBase.Load
            mycollection.Add("one", "two")
            Dim result As String = ""
            mycollection.TryGetValue("one", result)
            MessageBox.Show(result)
        End Sub
    End Class


    Success
    Cor


    Monday, May 21, 2012 5:07 AM
  • I tried using your dictionary example, but I still can't access the IndexOf method.

    Here's the compiler error:

    'IndexOf' is not a member of 'System.Collections.Generic.Dictionary(Of String, List_vs_Collection.test)'. (BC30456)

    ...




    Do you need both a 'key' and an 'index'?

    A Dictionary will allow you to access a collection by key. It is, by it's nature, unsorted, so getting an index is meaningless.

    If you need both, then you'll need to create a custom collection of some kind. If you just need an Index, then you can use a List() and create your own comparator (I'm not sure exactly how to do this, off the top of my head).

    If you have Classes instead of Structures, you can wrapper both the dictionary and a list in a custom class which gives you both an index and a keyed lookup. This can be very efficient, and gives you full control over the functionality.


    Stephen J Whiteley

    Monday, May 21, 2012 1:38 PM
    Moderator