473,782 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hashtable question

J L
I have defined a structure

private structure FieldInfo
dim FieldName as string
dim OrdinalPostioin as Integer
dim DataType as Type
dim Size as Integer
end structure

I read this information from a DataReader which retrieves schema info
from an Access table. (That piece of information is for background
only, does not affect my question).

I fill an arraylist with FieldInfo objects.

I then add this arraylist to a HashTable whose key is the name of the
Access table and value is the arraylist of FieldInfo objects. The
hashtable is named TableFields

I am having trouble retrieving the information. Here is the code I am
using :

dim testField as FieldInfo
dim testFieldList as ArrayList
dim strTableName as String
for i = 0 to TableList.Count - 1
strTableName = TableList(i)
testFieldList.C lear()
testFieldList = TableFields.Ite m(strTableName)
dim m as integer
for m = 0 to testFieldList.C ount - 1
testField = Ctype(testField List(m), FieldInfo)
messagebox.show ( testField.Field Name & vbcrlf & _
testField.DataT ype.ToString & vbcrlf & _
testField.Size. ToString)
next

The problem is that this shows the field information for the second
table as belonging to the first and has no field information for the
second one.

Am I using the correct syntax to access an ArrayList of FieldInfo data
stored in a HashTable?

Is there a better way to go about it? The bottom line is that I want
to have a list of field name, ordinal position, data type and size for
each field in each data table so I can do some validation in my DAL in
a generic way once I know a table name.

TIA,
John
Nov 21 '05 #1
7 1706
JL,

Do I see it right that you are creating a table with data that you want to
process using a key?

Cor
Nov 21 '05 #2

J L wrote:
[...]
I then add this arraylist to a HashTable whose key is the name of the
Access table and value is the arraylist of FieldInfo objects. The
hashtable is named TableFields

I am having trouble retrieving the information. Here is the code I am
using :

dim testField as FieldInfo
dim testFieldList as ArrayList
dim strTableName as String
for i = 0 to TableList.Count - 1
strTableName = TableList(i)
testFieldList.C lear()
testFieldList = TableFields.Ite m(strTableName) [...] The problem is that this shows the field information for the second
table as belonging to the first and has no field information for the
second one.

Am I using the correct syntax to access an ArrayList of FieldInfo data stored in a HashTable?
This looks fine; what we need to see also is the code where you load up
TableFields in the first place.

Is there a better way to go about it? The bottom line is that I want
to have a list of field name, ordinal position, data type and size for each field in each data table so I can do some validation in my DAL in a generic way once I know a table name.


A HashTable is a perfectly good way to store (key, value) information.

--
Larry Lard
Replies to group please

Nov 21 '05 #3
J L
Hi Cor,
No, I am not creating a table. I am reading the schema of the tables
in my Access database and trying to store the field information in a
HashTable whose key is the table name. I need to have one entry per
table in the HashTable with its Value object being an ArrayList that
contains a structure variable for each field in the table. The
strucuture variable holds the field name, ordinal position, data type
and size.

I will post my complete code in my second response.

Thanks,
John

On Fri, 1 Apr 2005 09:01:55 +0200, "Cor Ligthert"
<no************ @planet.nl> wrote:
JL,

Do I see it right that you are creating a table with data that you want to
process using a key?

Cor


Nov 21 '05 #4
J L
Thanks Cor and Larry for any help you can give me. Here is the code I
am using:

_______________ _______________ _______
Here are the declarations:

Private TableList As New ArrayList
Private TablePrimaryKey s As New Hashtable
Private TableFields As New Hashtable

Private Structure FieldInfo
Dim FieldName As String
Dim OrdinalPosition As Integer
Dim DataType As Type
Dim Size As Integer
End Structure

_______________ _______________ ______
Here is how I fill them:

Dim dt As DataTable
Dim dRow As DataRow
Dim dColumn As DataColumn
Dim aNull As DBNull
Dim aField As New FieldInfo
Dim fieldList As New ArrayList
Dim strPKList As String
Dim strTableName As String

Dim i As Integer

ConfigOpt.Initi alize(Applicati on.StartupPath & "\" &
Application.Pro ductName & ".cfg")
MarymonteDALCon nectString = ConfigOpt.GetOp tion("Connect String")
MarymonteDALDat aProvider = ConfigOpt.GetOp tion("DB Type")

TableList.Clear ()
TablePrimaryKey s.Clear()
TableFields.Cle ar()

Try
If MarymonteDALDat aProvider = "OleDb" Then
Dim cn As New OleDbConnection (MarymonteDALCo nnectString)
Dim cmd As New OleDbCommand
Dim myReader As OleDbDataReader

' test the connection string and read table info
cn.Open()
' get list of tables
Dim myNull() As Object = {aNull, aNull, aNull, "TABLE"}
dt = cn.GetOleDbSche maTable(OleDbSc hemaGuid.Tables , myNull)
Dim dr As DataRow
For Each dr In dt.Rows
TableList.Add(d r("TABLE_NAME") )
Next
' read table data
cmd.Connection = cn
For i = 0 To TableList.Count - 1
strTableName = TableList(i)
cmd.CommandText = "SELECT * FROM " & strTableName
myReader = cmd.ExecuteRead er(CommandBehav ior.KeyInfo)
dt = myReader.GetSch emaTable()
strPKList = ""
fieldList.Clear ()
For Each dRow In dt.Rows
aField.FieldNam e = dRow("ColumnNam e")
aField.OrdinalP osition = dRow("ColumnOrd inal")
aField.DataType = dRow("DataType" )
aField.Size = dRow("ColumnSiz e")
fieldList.Add(a Field)
If dRow("IsKey") Then
strPKList += dRow("ColumnNam e") & ";"
End If
Next
TableFields.Add (strTableName, fieldList)
TablePrimaryKey s.Add(strTableN ame, strPKList)
myReader.Close( )
Next

_______________ _______________ _______________ ___
And here is my test code to access them:

Private Sub DisplayTableInf oTest()
Dim i As Integer
Dim tstField As New FieldInfo
Dim tstFieldList As New ArrayList
Dim strTableName As String

Dim key As New Object
For Each key In TableFields
tstFieldList.Cl ear()
tstFieldList = CType(TableFiel ds.Item(key), ArrayList)
Dim n As Integer
For n = 0 To tstFieldList.Co unt - 1
tstField = CType(tstFieldL ist(n), FieldInfo)
MessageBox.Show (tstField.Field Name)
Next
Next
For i = 0 To TableList.Count - 1
strTableName = TableList(i)
tstFieldList.Cl ear()
tstFieldList = TableFields.Ite m(strTableName)
MessageBox.Show ("Primary keys for " & strTableName & ": " &
TablePrimaryKey s(strTableName) , strTableName)
Dim strTest As String
Dim m As Integer
For m = 0 To tstFieldList.Co unt - 1
tstField = CType(tstFieldL ist(m), FieldInfo)
strTest = ""
strTest += tstField.FieldN ame & vbCrLf & _
tstField.Ordina lPosition.ToStr ing & vbCrLf & _
tstField.DataTy pe.ToString & vbCrLf & _
tstField.Size.T oString
MessageBox.Show (strTest, TableList(i))
Next
Next
End Sub

_______________ _______________ _______________ _______

There are only two tables in the database (OpDetails and Operators).
OpDetails has 3 fields and Operators has 6. I see it fill the
ArrayList (fieldList) with the correct number of fields for each
table. And it saves that field list to the HashTable (TableFields).
Also to note, the othe HashTable (TablePrimaryKe ys) is filled
correctly and displays correctly.

In my test code this is what I see:
1. Displays the correct table names.
2. Displays the correct table primary keys (from the other HashTable I
filled).
3. Shows the 6 fields from Operators when accessing OpDetails (this is
first time through the display loop)
4. Shows no fields for Operators (second time through the loop)
5. I never see the 3 fields from OpDetails.

Seems like the fields for the second table processed (Operators) are
saved for the first table (OpDetails) and no fields saved for the
first table processed (OpDetails).

Hope this is not too confusing and somone can see my mistake.

TIA,
John
On 1 Apr 2005 01:48:31 -0800, "Larry Lard" <la*******@hotm ail.com>
wrote:

J L wrote:
[...]
I then add this arraylist to a HashTable whose key is the name of the
Access table and value is the arraylist of FieldInfo objects. The
hashtable is named TableFields

I am having trouble retrieving the information. Here is the code I am
using :

dim testField as FieldInfo
dim testFieldList as ArrayList
dim strTableName as String
for i = 0 to TableList.Count - 1
strTableName = TableList(i)
testFieldList.C lear()
testFieldList = TableFields.Ite m(strTableName)

[...]
The problem is that this shows the field information for the second
table as belonging to the first and has no field information for the
second one.

Am I using the correct syntax to access an ArrayList of FieldInfo

data
stored in a HashTable?


This looks fine; what we need to see also is the code where you load up
TableFields in the first place.

Is there a better way to go about it? The bottom line is that I want
to have a list of field name, ordinal position, data type and size

for
each field in each data table so I can do some validation in my DAL

in
a generic way once I know a table name.


A HashTable is a perfectly good way to store (key, value) information.


Nov 21 '05 #5
The problem is when you are attempting to store the 'fieldList' objects.

An ArrayList is a reference type. When you assign an instance of a reference
type to something you are assigning a reference to the source object, not a
copy of it.

The declaration Dim fieldList As New ArrayList is the only place where
fieldList is established as a 'new' object and all the later references to
it are references to the original object.

While loading, if you read the content of TableFields.Ite m(0) during the
second iteration of the outer loop, directly after after the
fieldList.Clear () line, you may be surprised to find that the 'fields' that
you saw get loaded are longer there. This is because what is stored in the
hash table is a reference to the object named fieldList. Follow my drift?

Now it gets a bit difficult and you have to do some mental juggling and it
took me me a little while to 'get it', but when you add the fieldlist object
to the hash table on the second iteration of you are again adding a
reference to the fieldlist object so you your hash table now has 2
references to the same object which now holds the 'fields' from the second
table. Still with me?

TableList and TablePrimaryKey s are OK because you are adding strings to
these hash tables and because a string is a value type the actual object
gets copied as opposed to a reference to it.

In the display loop you are creating yet another reference to the original
object - tstFieldList = CType(TableFiel ds.Item(key), ArrayList) - but on the
second iteration you're clearing the contents of the referenced object and
this is why you see no 'fields' on the second iteration.

There you have it - absolute clarity.
"J L" <jo**@marymonte .com> wrote in message
news:gp******** *************** *********@4ax.c om...
Thanks Cor and Larry for any help you can give me. Here is the code I
am using:

_______________ _______________ _______
Here are the declarations:

Private TableList As New ArrayList
Private TablePrimaryKey s As New Hashtable
Private TableFields As New Hashtable

Private Structure FieldInfo
Dim FieldName As String
Dim OrdinalPosition As Integer
Dim DataType As Type
Dim Size As Integer
End Structure

_______________ _______________ ______
Here is how I fill them:

Dim dt As DataTable
Dim dRow As DataRow
Dim dColumn As DataColumn
Dim aNull As DBNull
Dim aField As New FieldInfo
Dim fieldList As New ArrayList
Dim strPKList As String
Dim strTableName As String

Dim i As Integer

ConfigOpt.Initi alize(Applicati on.StartupPath & "\" &
Application.Pro ductName & ".cfg")
MarymonteDALCon nectString = ConfigOpt.GetOp tion("Connect String")
MarymonteDALDat aProvider = ConfigOpt.GetOp tion("DB Type")

TableList.Clear ()
TablePrimaryKey s.Clear()
TableFields.Cle ar()

Try
If MarymonteDALDat aProvider = "OleDb" Then
Dim cn As New OleDbConnection (MarymonteDALCo nnectString)
Dim cmd As New OleDbCommand
Dim myReader As OleDbDataReader

' test the connection string and read table info
cn.Open()
' get list of tables
Dim myNull() As Object = {aNull, aNull, aNull, "TABLE"}
dt = cn.GetOleDbSche maTable(OleDbSc hemaGuid.Tables , myNull)
Dim dr As DataRow
For Each dr In dt.Rows
TableList.Add(d r("TABLE_NAME") )
Next
' read table data
cmd.Connection = cn
For i = 0 To TableList.Count - 1
strTableName = TableList(i)
cmd.CommandText = "SELECT * FROM " & strTableName
myReader = cmd.ExecuteRead er(CommandBehav ior.KeyInfo)
dt = myReader.GetSch emaTable()
strPKList = ""
fieldList.Clear ()
For Each dRow In dt.Rows
aField.FieldNam e = dRow("ColumnNam e")
aField.OrdinalP osition = dRow("ColumnOrd inal")
aField.DataType = dRow("DataType" )
aField.Size = dRow("ColumnSiz e")
fieldList.Add(a Field)
If dRow("IsKey") Then
strPKList += dRow("ColumnNam e") & ";"
End If
Next
TableFields.Add (strTableName, fieldList)
TablePrimaryKey s.Add(strTableN ame, strPKList)
myReader.Close( )
Next

_______________ _______________ _______________ ___
And here is my test code to access them:

Private Sub DisplayTableInf oTest()
Dim i As Integer
Dim tstField As New FieldInfo
Dim tstFieldList As New ArrayList
Dim strTableName As String

Dim key As New Object
For Each key In TableFields
tstFieldList.Cl ear()
tstFieldList = CType(TableFiel ds.Item(key), ArrayList)
Dim n As Integer
For n = 0 To tstFieldList.Co unt - 1
tstField = CType(tstFieldL ist(n), FieldInfo)
MessageBox.Show (tstField.Field Name)
Next
Next
For i = 0 To TableList.Count - 1
strTableName = TableList(i)
tstFieldList.Cl ear()
tstFieldList = TableFields.Ite m(strTableName)
MessageBox.Show ("Primary keys for " & strTableName & ": " &
TablePrimaryKey s(strTableName) , strTableName)
Dim strTest As String
Dim m As Integer
For m = 0 To tstFieldList.Co unt - 1
tstField = CType(tstFieldL ist(m), FieldInfo)
strTest = ""
strTest += tstField.FieldN ame & vbCrLf & _
tstField.Ordina lPosition.ToStr ing & vbCrLf & _
tstField.DataTy pe.ToString & vbCrLf & _
tstField.Size.T oString
MessageBox.Show (strTest, TableList(i))
Next
Next
End Sub

_______________ _______________ _______________ _______

There are only two tables in the database (OpDetails and Operators).
OpDetails has 3 fields and Operators has 6. I see it fill the
ArrayList (fieldList) with the correct number of fields for each
table. And it saves that field list to the HashTable (TableFields).
Also to note, the othe HashTable (TablePrimaryKe ys) is filled
correctly and displays correctly.

In my test code this is what I see:
1. Displays the correct table names.
2. Displays the correct table primary keys (from the other HashTable I
filled).
3. Shows the 6 fields from Operators when accessing OpDetails (this is
first time through the display loop)
4. Shows no fields for Operators (second time through the loop)
5. I never see the 3 fields from OpDetails.

Seems like the fields for the second table processed (Operators) are
saved for the first table (OpDetails) and no fields saved for the
first table processed (OpDetails).

Hope this is not too confusing and somone can see my mistake.

TIA,
John
On 1 Apr 2005 01:48:31 -0800, "Larry Lard" <la*******@hotm ail.com>
wrote:

J L wrote:
[...]
I then add this arraylist to a HashTable whose key is the name of the
Access table and value is the arraylist of FieldInfo objects. The
hashtable is named TableFields

I am having trouble retrieving the information. Here is the code I am
using :

dim testField as FieldInfo
dim testFieldList as ArrayList
dim strTableName as String
for i = 0 to TableList.Count - 1
strTableName = TableList(i)
testFieldList.C lear()
testFieldList = TableFields.Ite m(strTableName)

[...]
The problem is that this shows the field information for the second
table as belonging to the first and has no field information for the
second one.

Am I using the correct syntax to access an ArrayList of FieldInfo

data
stored in a HashTable?


This looks fine; what we need to see also is the code where you load up
TableFields in the first place.

Is there a better way to go about it? The bottom line is that I want
to have a list of field name, ordinal position, data type and size

for
each field in each data table so I can do some validation in my DAL

in
a generic way once I know a table name.


A HashTable is a perfectly good way to store (key, value) information.

Nov 21 '05 #6
J L
Hi Stephany,
My PC ate my original response. I dont know if it will show up later
so here goes again...

You are fantastic! I did understand your explanation perfectly and
when I moved the declaration of the fieldList object into the loop
that was creating and filling it, it worked perfectly. Than you so
very much!

I am continually amazed and impressed by the talent and effort of
individuals on this NG. I am trying to contribute when possible but
want to say THANKS to all of you GURUS for both your expertise and
time! I am sure the Universe will reward you for your kindness.

John
On Sat, 2 Apr 2005 02:46:23 +1200, "Stephany Young" <noone@localhos t>
wrote:
The problem is when you are attempting to store the 'fieldList' objects.

An ArrayList is a reference type. When you assign an instance of a reference
type to something you are assigning a reference to the source object, not a
copy of it.

The declaration Dim fieldList As New ArrayList is the only place where
fieldList is established as a 'new' object and all the later references to
it are references to the original object.

While loading, if you read the content of TableFields.Ite m(0) during the
second iteration of the outer loop, directly after after the
fieldList.Clea r() line, you may be surprised to find that the 'fields' that
you saw get loaded are longer there. This is because what is stored in the
hash table is a reference to the object named fieldList. Follow my drift?

Now it gets a bit difficult and you have to do some mental juggling and it
took me me a little while to 'get it', but when you add the fieldlist object
to the hash table on the second iteration of you are again adding a
reference to the fieldlist object so you your hash table now has 2
references to the same object which now holds the 'fields' from the second
table. Still with me?

TableList and TablePrimaryKey s are OK because you are adding strings to
these hash tables and because a string is a value type the actual object
gets copied as opposed to a reference to it.

In the display loop you are creating yet another reference to the original
object - tstFieldList = CType(TableFiel ds.Item(key), ArrayList) - but on the
second iteration you're clearing the contents of the referenced object and
this is why you see no 'fields' on the second iteration.

There you have it - absolute clarity.
"J L" <jo**@marymonte .com> wrote in message
news:gp******* *************** **********@4ax. com...
Thanks Cor and Larry for any help you can give me. Here is the code I
am using:

_______________ _______________ _______
Here are the declarations:

Private TableList As New ArrayList
Private TablePrimaryKey s As New Hashtable
Private TableFields As New Hashtable

Private Structure FieldInfo
Dim FieldName As String
Dim OrdinalPosition As Integer
Dim DataType As Type
Dim Size As Integer
End Structure

_______________ _______________ ______
Here is how I fill them:

Dim dt As DataTable
Dim dRow As DataRow
Dim dColumn As DataColumn
Dim aNull As DBNull
Dim aField As New FieldInfo
Dim fieldList As New ArrayList
Dim strPKList As String
Dim strTableName As String

Dim i As Integer

ConfigOpt.Initi alize(Applicati on.StartupPath & "\" &
Application.Pro ductName & ".cfg")
MarymonteDALCon nectString = ConfigOpt.GetOp tion("Connect String")
MarymonteDALDat aProvider = ConfigOpt.GetOp tion("DB Type")

TableList.Clear ()
TablePrimaryKey s.Clear()
TableFields.Cle ar()

Try
If MarymonteDALDat aProvider = "OleDb" Then
Dim cn As New OleDbConnection (MarymonteDALCo nnectString)
Dim cmd As New OleDbCommand
Dim myReader As OleDbDataReader

' test the connection string and read table info
cn.Open()
' get list of tables
Dim myNull() As Object = {aNull, aNull, aNull, "TABLE"}
dt = cn.GetOleDbSche maTable(OleDbSc hemaGuid.Tables , myNull)
Dim dr As DataRow
For Each dr In dt.Rows
TableList.Add(d r("TABLE_NAME") )
Next
' read table data
cmd.Connection = cn
For i = 0 To TableList.Count - 1
strTableName = TableList(i)
cmd.CommandText = "SELECT * FROM " & strTableName
myReader = cmd.ExecuteRead er(CommandBehav ior.KeyInfo)
dt = myReader.GetSch emaTable()
strPKList = ""
fieldList.Clear ()
For Each dRow In dt.Rows
aField.FieldNam e = dRow("ColumnNam e")
aField.OrdinalP osition = dRow("ColumnOrd inal")
aField.DataType = dRow("DataType" )
aField.Size = dRow("ColumnSiz e")
fieldList.Add(a Field)
If dRow("IsKey") Then
strPKList += dRow("ColumnNam e") & ";"
End If
Next
TableFields.Add (strTableName, fieldList)
TablePrimaryKey s.Add(strTableN ame, strPKList)
myReader.Close( )
Next

_______________ _______________ _______________ ___
And here is my test code to access them:

Private Sub DisplayTableInf oTest()
Dim i As Integer
Dim tstField As New FieldInfo
Dim tstFieldList As New ArrayList
Dim strTableName As String

Dim key As New Object
For Each key In TableFields
tstFieldList.Cl ear()
tstFieldList = CType(TableFiel ds.Item(key), ArrayList)
Dim n As Integer
For n = 0 To tstFieldList.Co unt - 1
tstField = CType(tstFieldL ist(n), FieldInfo)
MessageBox.Show (tstField.Field Name)
Next
Next
For i = 0 To TableList.Count - 1
strTableName = TableList(i)
tstFieldList.Cl ear()
tstFieldList = TableFields.Ite m(strTableName)
MessageBox.Show ("Primary keys for " & strTableName & ": " &
TablePrimaryKey s(strTableName) , strTableName)
Dim strTest As String
Dim m As Integer
For m = 0 To tstFieldList.Co unt - 1
tstField = CType(tstFieldL ist(m), FieldInfo)
strTest = ""
strTest += tstField.FieldN ame & vbCrLf & _
tstField.Ordina lPosition.ToStr ing & vbCrLf & _
tstField.DataTy pe.ToString & vbCrLf & _
tstField.Size.T oString
MessageBox.Show (strTest, TableList(i))
Next
Next
End Sub

_______________ _______________ _______________ _______

There are only two tables in the database (OpDetails and Operators).
OpDetails has 3 fields and Operators has 6. I see it fill the
ArrayList (fieldList) with the correct number of fields for each
table. And it saves that field list to the HashTable (TableFields).
Also to note, the othe HashTable (TablePrimaryKe ys) is filled
correctly and displays correctly.

In my test code this is what I see:
1. Displays the correct table names.
2. Displays the correct table primary keys (from the other HashTable I
filled).
3. Shows the 6 fields from Operators when accessing OpDetails (this is
first time through the display loop)
4. Shows no fields for Operators (second time through the loop)
5. I never see the 3 fields from OpDetails.

Seems like the fields for the second table processed (Operators) are
saved for the first table (OpDetails) and no fields saved for the
first table processed (OpDetails).

Hope this is not too confusing and somone can see my mistake.

TIA,
John
On 1 Apr 2005 01:48:31 -0800, "Larry Lard" <la*******@hotm ail.com>
wrote:

J L wrote:
[...]
I then add this arraylist to a HashTable whose key is the name of the
Access table and value is the arraylist of FieldInfo objects. The
hashtable is named TableFields

I am having trouble retrieving the information. Here is the code I am
using :

dim testField as FieldInfo
dim testFieldList as ArrayList
dim strTableName as String
for i = 0 to TableList.Count - 1
strTableName = TableList(i)
testFieldList.C lear()
testFieldList = TableFields.Ite m(strTableName)
[...]
The problem is that this shows the field information for the second
table as belonging to the first and has no field information for the
second one.

Am I using the correct syntax to access an ArrayList of FieldInfo
data
stored in a HashTable?

This looks fine; what we need to see also is the code where you load up
TableField s in the first place.
Is there a better way to go about it? The bottom line is that I want
to have a list of field name, ordinal position, data type and size
for
each field in each data table so I can do some validation in my DAL
in
a generic way once I know a table name.

A HashTable is a perfectly good way to store (key, value) information.


Nov 21 '05 #7
J L
OMG!!! You are fantastic! I do understand it with extreme clarity.
Once I moved the declaration of fieldList into my loop, it worked
fine. As for the display routine, the part where I was clearing the
object I had actually discarded already. And now the Universe is in
order.

I have to say that I am continually amazed at the quality of
individuals on this NG. I try to contribute when I can but thanks to
you and all the other GURUS for your support!!

John

On Sat, 2 Apr 2005 02:46:23 +1200, "Stephany Young" <noone@localhos t>
wrote:
The problem is when you are attempting to store the 'fieldList' objects.

An ArrayList is a reference type. When you assign an instance of a reference
type to something you are assigning a reference to the source object, not a
copy of it.

The declaration Dim fieldList As New ArrayList is the only place where
fieldList is established as a 'new' object and all the later references to
it are references to the original object.

While loading, if you read the content of TableFields.Ite m(0) during the
second iteration of the outer loop, directly after after the
fieldList.Clea r() line, you may be surprised to find that the 'fields' that
you saw get loaded are longer there. This is because what is stored in the
hash table is a reference to the object named fieldList. Follow my drift?

Now it gets a bit difficult and you have to do some mental juggling and it
took me me a little while to 'get it', but when you add the fieldlist object
to the hash table on the second iteration of you are again adding a
reference to the fieldlist object so you your hash table now has 2
references to the same object which now holds the 'fields' from the second
table. Still with me?

TableList and TablePrimaryKey s are OK because you are adding strings to
these hash tables and because a string is a value type the actual object
gets copied as opposed to a reference to it.

In the display loop you are creating yet another reference to the original
object - tstFieldList = CType(TableFiel ds.Item(key), ArrayList) - but on the
second iteration you're clearing the contents of the referenced object and
this is why you see no 'fields' on the second iteration.

There you have it - absolute clarity.
"J L" <jo**@marymonte .com> wrote in message
news:gp******* *************** **********@4ax. com...
Thanks Cor and Larry for any help you can give me. Here is the code I
am using:

_______________ _______________ _______
Here are the declarations:

Private TableList As New ArrayList
Private TablePrimaryKey s As New Hashtable
Private TableFields As New Hashtable

Private Structure FieldInfo
Dim FieldName As String
Dim OrdinalPosition As Integer
Dim DataType As Type
Dim Size As Integer
End Structure

_______________ _______________ ______
Here is how I fill them:

Dim dt As DataTable
Dim dRow As DataRow
Dim dColumn As DataColumn
Dim aNull As DBNull
Dim aField As New FieldInfo
Dim fieldList As New ArrayList
Dim strPKList As String
Dim strTableName As String

Dim i As Integer

ConfigOpt.Initi alize(Applicati on.StartupPath & "\" &
Application.Pro ductName & ".cfg")
MarymonteDALCon nectString = ConfigOpt.GetOp tion("Connect String")
MarymonteDALDat aProvider = ConfigOpt.GetOp tion("DB Type")

TableList.Clear ()
TablePrimaryKey s.Clear()
TableFields.Cle ar()

Try
If MarymonteDALDat aProvider = "OleDb" Then
Dim cn As New OleDbConnection (MarymonteDALCo nnectString)
Dim cmd As New OleDbCommand
Dim myReader As OleDbDataReader

' test the connection string and read table info
cn.Open()
' get list of tables
Dim myNull() As Object = {aNull, aNull, aNull, "TABLE"}
dt = cn.GetOleDbSche maTable(OleDbSc hemaGuid.Tables , myNull)
Dim dr As DataRow
For Each dr In dt.Rows
TableList.Add(d r("TABLE_NAME") )
Next
' read table data
cmd.Connection = cn
For i = 0 To TableList.Count - 1
strTableName = TableList(i)
cmd.CommandText = "SELECT * FROM " & strTableName
myReader = cmd.ExecuteRead er(CommandBehav ior.KeyInfo)
dt = myReader.GetSch emaTable()
strPKList = ""
fieldList.Clear ()
For Each dRow In dt.Rows
aField.FieldNam e = dRow("ColumnNam e")
aField.OrdinalP osition = dRow("ColumnOrd inal")
aField.DataType = dRow("DataType" )
aField.Size = dRow("ColumnSiz e")
fieldList.Add(a Field)
If dRow("IsKey") Then
strPKList += dRow("ColumnNam e") & ";"
End If
Next
TableFields.Add (strTableName, fieldList)
TablePrimaryKey s.Add(strTableN ame, strPKList)
myReader.Close( )
Next

_______________ _______________ _______________ ___
And here is my test code to access them:

Private Sub DisplayTableInf oTest()
Dim i As Integer
Dim tstField As New FieldInfo
Dim tstFieldList As New ArrayList
Dim strTableName As String

Dim key As New Object
For Each key In TableFields
tstFieldList.Cl ear()
tstFieldList = CType(TableFiel ds.Item(key), ArrayList)
Dim n As Integer
For n = 0 To tstFieldList.Co unt - 1
tstField = CType(tstFieldL ist(n), FieldInfo)
MessageBox.Show (tstField.Field Name)
Next
Next
For i = 0 To TableList.Count - 1
strTableName = TableList(i)
tstFieldList.Cl ear()
tstFieldList = TableFields.Ite m(strTableName)
MessageBox.Show ("Primary keys for " & strTableName & ": " &
TablePrimaryKey s(strTableName) , strTableName)
Dim strTest As String
Dim m As Integer
For m = 0 To tstFieldList.Co unt - 1
tstField = CType(tstFieldL ist(m), FieldInfo)
strTest = ""
strTest += tstField.FieldN ame & vbCrLf & _
tstField.Ordina lPosition.ToStr ing & vbCrLf & _
tstField.DataTy pe.ToString & vbCrLf & _
tstField.Size.T oString
MessageBox.Show (strTest, TableList(i))
Next
Next
End Sub

_______________ _______________ _______________ _______

There are only two tables in the database (OpDetails and Operators).
OpDetails has 3 fields and Operators has 6. I see it fill the
ArrayList (fieldList) with the correct number of fields for each
table. And it saves that field list to the HashTable (TableFields).
Also to note, the othe HashTable (TablePrimaryKe ys) is filled
correctly and displays correctly.

In my test code this is what I see:
1. Displays the correct table names.
2. Displays the correct table primary keys (from the other HashTable I
filled).
3. Shows the 6 fields from Operators when accessing OpDetails (this is
first time through the display loop)
4. Shows no fields for Operators (second time through the loop)
5. I never see the 3 fields from OpDetails.

Seems like the fields for the second table processed (Operators) are
saved for the first table (OpDetails) and no fields saved for the
first table processed (OpDetails).

Hope this is not too confusing and somone can see my mistake.

TIA,
John
On 1 Apr 2005 01:48:31 -0800, "Larry Lard" <la*******@hotm ail.com>
wrote:

J L wrote:
[...]
I then add this arraylist to a HashTable whose key is the name of the
Access table and value is the arraylist of FieldInfo objects. The
hashtable is named TableFields

I am having trouble retrieving the information. Here is the code I am
using :

dim testField as FieldInfo
dim testFieldList as ArrayList
dim strTableName as String
for i = 0 to TableList.Count - 1
strTableName = TableList(i)
testFieldList.C lear()
testFieldList = TableFields.Ite m(strTableName)
[...]
The problem is that this shows the field information for the second
table as belonging to the first and has no field information for the
second one.

Am I using the correct syntax to access an ArrayList of FieldInfo
data
stored in a HashTable?

This looks fine; what we need to see also is the code where you load up
TableField s in the first place.
Is there a better way to go about it? The bottom line is that I want
to have a list of field name, ordinal position, data type and size
for
each field in each data table so I can do some validation in my DAL
in
a generic way once I know a table name.

A HashTable is a perfectly good way to store (key, value) information.


Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8717
by: francois | last post by:
Hi, I have a webservice that I am using and I would like it to return an XML serialized version of an object. the class of the object is defined serializable as the following: public class Event {
5
2831
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would like it to return an XML serialized version of an object.
5
15584
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable. One function of the Hashtable is to iterate through its keys, which apparently is inherently not thread-safe. Other functions of the Hashtable include adding/modifying/deleting. To solve the synchronization issues I am doing two things: 1. Lock...
33
3323
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are used before adding, removing or iterating the Hashtable. The program runs in a high workload environment. After running a few days, now it suddenly catchs this Exception when inserting a pair of key and object, stacktrace =...
5
1509
by: ad | last post by:
I have a hashtable name myHash. sKey is a string, I use myHash to retrieve the value of that key. But is sKey is not in the myHash.Keys, it raise an Exception. I want to dertiminate if sKey in myHash.Keys. How can I do that?
2
5544
by: Ali | last post by:
I am binding a hashTable to a dropDownList to pick a State (key: like New York) and sends the state designation (value: NY) to a filtering procedure. I have entered the states in the hashTable in alphabetical order, but the dropDownList renders them in a non-order list. code snippet: Dim state as New hashTable state.Add("Alabama", "AL") state.Add("Alaska", "AK")
8
1691
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I can write myTable.ContainsKey ( 123 ). Or do the keys all have to be of...
4
3789
by: Macca | last post by:
Hi, I am using a hashtable as a cache. I am only allowed for the cache to take up a certain amount of memory. I think I can state how large I want the hashtable to be on creation. What I need is someway to monitor the size of the hashtable so that as it nears the maximum size I have set, I can delete some item or empty the table altogether.
2
3152
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I need to write to it, but now it occurred to me that maybe I could just lock one of the Hashtables inside without locking the entire object, but then I thought maybe some thread could instruct the outside Hashtable to remove an inside Hashtable...
2
3881
by: archana | last post by:
Hi all, I am having one confusion regarding hashtable. I am having function in which i am passing hashtable as reference. In function i am creating one hashtable which is local to that function. Then i am setting this hash table to hashtable which i am passing as ref. So my question is how scope is mention when i am assigning local
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.