Adding to a collection in Net vs VB6 ...

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

Checking whether an item exists in a collection before adding it ... (Key is
always a string and in this case is the value as well)

As Net has a Contains method ... is the following a safe alternative to the
VB6 On Error Resume Next?

If Not colDs.Contains(sKey) Then
colIDs.Add(sKey, sKey)
End If

Thanks,
Mike
 
Checking whether an item exists in a collection before adding it ... (Key is
always a string and in this case is the value as well)

As Net has a Contains method ... is the following a safe alternative to the
VB6 On Error Resume Next?

If Not colDs.Contains(sKey) Then
colIDs.Add(sKey, sKey)
End If

Thanks,
Mike

Well, assuming this is a Dictionary (Of String, Of String) you would
use:

If Not colDs.ContainsKey(sKey) Then
....
End If

And if it's a System.Collections.Hashtable - then the code is the same
:)
 
Mike,

In VB6 you have one collection (which is as well in VB for Net and not in
C#). However most of us here have the opinion that it is the only thing that
should be as possible avoided from the Net namespace Microsoft.VisualBasic
(this is a true part of Net).

In .Net there are endless collections, lists, special collections, generic
collections etc.

In this case you can start with the arraylist, however better is a simple
List(of String) because then the strings are easier to use.

http://msdn2.microsoft.com/en-us/library/6sh2ey19(vs.80).aspx

Cor
 
Thanks very much for all the replies.

The code which I'm converting from VB6 uses a number of simple collections
and the On Error Resume Next statement to handle duplicate keys. As I'm sure
you know, this was a common VB6 practice.

What I was wondering is whether the Net methods Contains/ContainsKey are
regarded as a safe alternative to the Resume Next error handling. It seems a
little too simple, so I was wondering if I'm missing some gotcha here ...
 
Mike,

The Resume next has *seldom* been a good handling of known situations
(As the try and catch are as well not).

The one you use now is preventing not wanted situations, whereabout you have
full control.

Cor
 
Back
Top