Determine if an item is in a Collection

G

Guest

I've created a lookup class for a windows form application and I'm stuck on
how on using the collection object. The process is simple, if the item is in
the collection then return that object, if the item is not in the collection
then create the item from a value in the database, then return it.

Here is the code:

Imports System.Data.SqlClient

Public Class lookup
Private Shared m_lookup As New Collection

Public Sub New()

End Sub

Public Shared ReadOnly Property LookupValueDataTable(ByVal LookupType As
String) As DataTable
Get
Return GetLookupValue(LookupType)
End Get
End Property

Private Shared Sub PopulateLookupAsDataTable(ByVal LookupType As String)
Dim result As DataTable

'Sets the first table of DataSet to result Datatable
result = SqlHelper.ExecuteDataset(Properties.getConnString,
"getLookupByType_sp", LookupType).Tables(0)

m_lookup.Add(result, LookupType)
End Sub

Private Shared Function GetLookupValue(ByVal LookupType As String) As
DataTable
'If item is not in the Collection then look up value and return,
else return value

If m_lookup.Item(LookupType) Is Nothing Then 'ERROR LINE
Return CType(m_lookup.Item(LookupType), DataTable)
Else
PopulateLookupAsDataTable(LookupType)
Return CType(m_lookup.Item(LookupType), DataTable)
End If

End Function

End Class

The problem is when I try to determine if the item exists in the collection,
I've tried a number of things, including what I have above which is checking
if the object is equal to nothing. I've also tried using DirectCast:
DirectCast(m_lookup, IList).Contains(LookupType)
but it always returns false.

I have Option Strict on in case you offer something that might conflict with
this setting. Thanks.

Josh.
 
M

Matt

Basically, all you'll have to do is get

yourcollection.Contains(thekey)

and that will return a boolean value saying whether or not the
collection contains that item.
 
G

Guest

Doesn't work, I receive the following error:
'Contains' is not a member of 'Microsoft.VisualBasic.Collection'.

Josh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top