473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Collection in a custom class

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
2 1331
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sundown | last post by:
I am trying to create a custom button control for the web that, when clicked, disables and changes the text of itself and a bunch of other controls (in the collection). My goal is to end up with a...
2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
9
by: craig | last post by:
Assume that you would like to create a custom collection class that is: 1. Strongly-typed (only holds Customer objects) 2. Read-only (user cannot add Customer objects) 3. Able to be bound to...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
1
by: Jeff S | last post by:
I'm storing a list of widgets in a database. The list changes infrequently (twice per week at most), and is relatively short (200 items at most, with very little detail per item). A small subset of...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
6
by: Erick | last post by:
I've created a class called Procs and a collection class called Processes which uses a hastable object to store the Procs. Now i want to enumerate with the "For each" to extract all the Procs in...
7
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int,...
7
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
Can someone explain a few things about collections to me. In C# 2 generics, you can create a collection class by inheriting from System.Collections.ObjectModel.Collection. Using this you can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.