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

Collection in a custom class

P: n/a
The code below is a class I am creating in order to help facilitate a
generic "data layer" assembly. Anyway, as you see the RecordCriteria class
contains three hashtables, and a structure called RecordCriteriaItem. I
want to be able to use a for each loop to loop through the values in the
hash tables (as you can see, each RecordCriteriaItem consists of the related
objects in the three Hashtables). Something like this:

Dim rc as RecordCriteria
....
Dim item as RecordCriteriaItem
For Each item in rc
...
Next

I have no idea how to accomplish this....
THE CODE!!!!-------------------------------------------------------------

Public Class RecordCriteria

Dim OpCodes As Hashtable
Dim Values As Hashtable
Dim IsNumber As Hashtable

Structure RecordCriteriaItem
Dim RemoteFieldName As String
Dim OpCode As String
Dim Value As Object
Dim IsNumber As Boolean
End Structure

Sub New()
If OpCodes Is Nothing Then OpCodes = New Hashtable
If Values Is Nothing Then Values = New Hashtable
If IsNumber Is Nothing Then IsNumber = New Hashtable
End Sub

Sub Add(ByVal RemoteFieldName As String, ByVal Opcode As String, ByVal
Value As Object, ByVal isnumeric As Boolean)
OpCodes.Add(RemoteFieldName, Opcode)
Values.Add(RemoteFieldName, Value)
IsNumber.Add(RemoteFieldName, IsNumber)
End Sub

Sub Remove(ByVal RemoteFieldName As String)
OpCodes.Remove(RemoteFieldName)
Values.Remove(RemoteFieldName)
IsNumber.Remove(RemoteFieldName)
End Sub

Function GetItem(ByVal remotefieldname As String) As RecordCriteriaItem
GetItem.IsNumber = IsNumber(remotefieldname)
GetItem.OpCode = OpCodes(remotefieldname)
GetItem.RemoteFieldName = remotefieldname
GetItem.Value = Values(remotefieldname)
Return GetItem
End Function

Function GetSQLWHERECriteria(ByVal remotefieldname As String) As String
Dim Text As String = "("
Text = Text & remotefieldname
Text = Text & " "
Text = Text & OpCodes(remotefieldname)
Text = Text & " "
If IsNumber(remotefieldname) <> True Then Text = Text & "'"
Text = Text & Values(remotefieldname)
If IsNumber(remotefieldname) <> True Then Text = Text & "'"
Text = Text & ")"
End Function

Function GetFullSQLWHEREClause() As String
Dim Text As String = " WHERE "
Dim i As IDictionaryEnumerator
For Each i In Values
Text = Text & "(" & i.Key & " = "
If IsNumber(i.Key) <> True Then Text = Text & "'"
Text = Text & i.Value
If IsNumber(i.Key) <> True Then Text = Text & "') AND "
Next
Text = Mid(Text, 1, Len(Text) - 4)
Return Text
End Function

End Class


Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You need to implement IEnumerable or inherit from CollectionBase, but from
the looks of it, all the hard stuff is done already.
"Rick Palmer" <ri*********@welove.com.nospam> wrote in message
news:OZ**************@TK2MSFTNGP10.phx.gbl...
The code below is a class I am creating in order to help facilitate a
generic "data layer" assembly. Anyway, as you see the RecordCriteria class contains three hashtables, and a structure called RecordCriteriaItem. I
want to be able to use a for each loop to loop through the values in the
hash tables (as you can see, each RecordCriteriaItem consists of the related objects in the three Hashtables). Something like this:

Dim rc as RecordCriteria
...
Dim item as RecordCriteriaItem
For Each item in rc
...
Next

I have no idea how to accomplish this....
THE CODE!!!!-------------------------------------------------------------

Public Class RecordCriteria

Dim OpCodes As Hashtable
Dim Values As Hashtable
Dim IsNumber As Hashtable

Structure RecordCriteriaItem
Dim RemoteFieldName As String
Dim OpCode As String
Dim Value As Object
Dim IsNumber As Boolean
End Structure

Sub New()
If OpCodes Is Nothing Then OpCodes = New Hashtable
If Values Is Nothing Then Values = New Hashtable
If IsNumber Is Nothing Then IsNumber = New Hashtable
End Sub

Sub Add(ByVal RemoteFieldName As String, ByVal Opcode As String, ByVal
Value As Object, ByVal isnumeric As Boolean)
OpCodes.Add(RemoteFieldName, Opcode)
Values.Add(RemoteFieldName, Value)
IsNumber.Add(RemoteFieldName, IsNumber)
End Sub

Sub Remove(ByVal RemoteFieldName As String)
OpCodes.Remove(RemoteFieldName)
Values.Remove(RemoteFieldName)
IsNumber.Remove(RemoteFieldName)
End Sub

Function GetItem(ByVal remotefieldname As String) As RecordCriteriaItem GetItem.IsNumber = IsNumber(remotefieldname)
GetItem.OpCode = OpCodes(remotefieldname)
GetItem.RemoteFieldName = remotefieldname
GetItem.Value = Values(remotefieldname)
Return GetItem
End Function

Function GetSQLWHERECriteria(ByVal remotefieldname As String) As String Dim Text As String = "("
Text = Text & remotefieldname
Text = Text & " "
Text = Text & OpCodes(remotefieldname)
Text = Text & " "
If IsNumber(remotefieldname) <> True Then Text = Text & "'"
Text = Text & Values(remotefieldname)
If IsNumber(remotefieldname) <> True Then Text = Text & "'"
Text = Text & ")"
End Function

Function GetFullSQLWHEREClause() As String
Dim Text As String = " WHERE "
Dim i As IDictionaryEnumerator
For Each i In Values
Text = Text & "(" & i.Key & " = "
If IsNumber(i.Key) <> True Then Text = Text & "'"
Text = Text & i.Value
If IsNumber(i.Key) <> True Then Text = Text & "') AND "
Next
Text = Mid(Text, 1, Len(Text) - 4)
Return Text
End Function

End Class

Nov 20 '05 #2

P: n/a
Rick,
Looking at your code you will need to implement IEnumerable in your class,
plus you may need to create a second class that implements IEnumerator. This
second class would return RecordCriteriaItem objects for each element in
your hashtables.

Is there a reason you have three hashtables, as oppose to a single hashtable
that stores the structure itself?

I would suggest you have RecordCriteria inherit from DictionaryBase (as
DictionaryBase encapsulates a hashtable), then have RecordCriteria store
RecordCriteriaItem objects directly.

Something like:

Public Structure RecordCriteriaItem
Dim RemoteFieldName As String
Dim OpCode As String
Dim Value As Object
Dim IsNumber As Boolean
End Structure

Public Class RecordCriteria
Inherits DictionaryBase

Public Sub Add(ByVal RemoteFieldName As String, ByVal Opcode As
String, ByVal Value As Object, ByVal isnumeric As Boolean)
Dim item As RecordCriteriaItem
item.RemoteFieldName = RemoteFieldName
item.OpCode = Opcode
item.Value = Value
item.IsNumber = isnumeric
Me.InnerHashtable.Add(RemoteFieldName, item)
End Sub

Public Sub Remove(ByVal RemoteFieldName As String)
Me.InnerHashtable.Remove(RemoteFieldName)
End Sub

Default Public ReadOnly Property Item(ByVal RemoteFieldName As
String) As RecordCriteriaItem
Get
Return DirectCast(Me.InnerHashtable(RemoteFieldName),
RecordCriteriaItem)
End Get
End Property

End Class

Note DictionaryBase automatically gives you For Each! Plus a number of other
properties that you expect on collections. Notice that I changed your
GetItem to a default property, this allows you to:

Dim rc as RecordCriteria
Dim item as RecordCriteriaItem

item = rc("field1")

or you can still:

item = rc.Item("field1")

I will let you implement GetSQLWHERECriteria & GetFullSQLWHEREClause, I
would suggest you look at using a System.Text.StringBuilder class instead of
String Concatenation in them.

Hope this helps
Jay

"Rick Palmer" <ri*********@welove.com.nospam> wrote in message
news:OZ**************@TK2MSFTNGP10.phx.gbl...
The code below is a class I am creating in order to help facilitate a
generic "data layer" assembly. Anyway, as you see the RecordCriteria class contains three hashtables, and a structure called RecordCriteriaItem. I
want to be able to use a for each loop to loop through the values in the
hash tables (as you can see, each RecordCriteriaItem consists of the related objects in the three Hashtables). Something like this:

Dim rc as RecordCriteria
...
Dim item as RecordCriteriaItem
For Each item in rc
...
Next

I have no idea how to accomplish this....
THE CODE!!!!-------------------------------------------------------------

Public Class RecordCriteria

Dim OpCodes As Hashtable
Dim Values As Hashtable
Dim IsNumber As Hashtable

Structure RecordCriteriaItem
Dim RemoteFieldName As String
Dim OpCode As String
Dim Value As Object
Dim IsNumber As Boolean
End Structure

Sub New()
If OpCodes Is Nothing Then OpCodes = New Hashtable
If Values Is Nothing Then Values = New Hashtable
If IsNumber Is Nothing Then IsNumber = New Hashtable
End Sub

Sub Add(ByVal RemoteFieldName As String, ByVal Opcode As String, ByVal
Value As Object, ByVal isnumeric As Boolean)
OpCodes.Add(RemoteFieldName, Opcode)
Values.Add(RemoteFieldName, Value)
IsNumber.Add(RemoteFieldName, IsNumber)
End Sub

Sub Remove(ByVal RemoteFieldName As String)
OpCodes.Remove(RemoteFieldName)
Values.Remove(RemoteFieldName)
IsNumber.Remove(RemoteFieldName)
End Sub

Function GetItem(ByVal remotefieldname As String) As RecordCriteriaItem GetItem.IsNumber = IsNumber(remotefieldname)
GetItem.OpCode = OpCodes(remotefieldname)
GetItem.RemoteFieldName = remotefieldname
GetItem.Value = Values(remotefieldname)
Return GetItem
End Function

Function GetSQLWHERECriteria(ByVal remotefieldname As String) As String Dim Text As String = "("
Text = Text & remotefieldname
Text = Text & " "
Text = Text & OpCodes(remotefieldname)
Text = Text & " "
If IsNumber(remotefieldname) <> True Then Text = Text & "'"
Text = Text & Values(remotefieldname)
If IsNumber(remotefieldname) <> True Then Text = Text & "'"
Text = Text & ")"
End Function

Function GetFullSQLWHEREClause() As String
Dim Text As String = " WHERE "
Dim i As IDictionaryEnumerator
For Each i In Values
Text = Text & "(" & i.Key & " = "
If IsNumber(i.Key) <> True Then Text = Text & "'"
Text = Text & i.Value
If IsNumber(i.Key) <> True Then Text = Text & "') AND "
Next
Text = Mid(Text, 1, Len(Text) - 4)
Return Text
End Function

End Class

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.