Checking for Existing Key in Collection

  • Thread starter Thread starter Blake Weaver
  • Start date Start date
B

Blake Weaver

Subject pretty much says it. I'm testing my Collection to check and see if
an item has already been added, thus avoiding duplicates. I don't even know
what value to check it against... some sort of Null I'd guess. I tried
IsNothing(MyCollection("key") but I get the following error:

System.ArgumentException: Argument 'Index' is not a valid value.

Not sure what I'm missing.

Thanks,
Blake
 
Blake Weaver said:
Subject pretty much says it. I'm testing my Collection to check and
see if an item has already been added, thus avoiding duplicates. I
don't even know what value to check it against... some sort of Null
I'd guess. I tried IsNothing(MyCollection("key") but I get the
following error:

System.ArgumentException: Argument 'Index' is not a valid value.

Not sure what I'm missing.

What is "my Collection"? Which type? What is it derived from? Most
collections have a Contains function (boolean), or a IndexOf function
returning -1 if the item is not found.
 
Unfortunately its just a quick Microsoft.VisualBasic.Collection variable I
dimensioned. I was hoping to avoid creating a real collection class for
this. Anyway, a variable of this type doesn't have 'Contains' nor 'IndexOf'
as options.

Thanks,
Blake
 
Blake Weaver said:
Unfortunately its just a quick Microsoft.VisualBasic.Collection
variable I dimensioned. I was hoping to avoid creating a real
collection class for this. Anyway, a variable of this type doesn't
have 'Contains' nor 'IndexOf' as options.

Microsoft.VisualBasic.Collection implements System.Collections.IList:

if directcast(yourcollection, ilist).contains("item") then

end if
 
Hi Armin,
Microsoft.VisualBasic.Collection implements System.Collections.IList:

Are you sure of this, I have always thought that this was one of the
exceptions?

Cor
 
Cor

I do think the Collection class implements the IList:
Dim c As New Collection
If GetType(IList).IsInstanceOfType(c) Then
MessageBox.Show("IList")
Else
MessageBox.Show("No IList")
End If

=> Gives me "IList".

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
Hi Jan,

I was always thinking that it was not, however when Armin write something as
this I become in doubt, but not in doubt enough to do what you did.

I was so quiet sure that it was not (because of the One index) that I only
did look in the documentation and did not check it, mostly I do, but now I
know I had to check it.

And I find it always funny when Armin starts to correct me.
And this was a change to give him a complete victory for a time.
So the trouble was not that big if it was.

:-))

I was avoiding the collection because of that.

Thanks,

Cor
 
Cor said:
And I find it always funny when Armin starts to correct me.
And this was a change to give him a complete victory for a time.

"Victory"? Why do people here think I am *so* bad? boooooohoooohoooooo :.-(

;-)))
 
Back
Top