By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,728 Members | 1,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,728 IT Pros & Developers. It's quick & easy.

Determine if an item is in a Collection

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
jmhmaine wrote:
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.

Nov 21 '05 #2

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

Josh.

"Matt" wrote:
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.
jmhmaine wrote:
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.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.