Solved

Add HashTable as Value inside another Hashtable

Posted on 2006-11-21
2
191 Views
Last Modified: 2010-04-23
How can I reuse htFields inside the For Each loop?

I want to add htFields as the value for the matching key inside htTables




    dim htTables as New Hashtable()

    for each str as String in GetTableNames(conn)
      debug.WriteLine(str)
      htTables.Add(str,Nothing)
      conn.Open
      Dim cmd1 As OleDb.OleDbCommand = New OleDb.OleDbCommand("select * from " & str, conn)
      Dim dra As IDataReader = cmd1.ExecuteReader(CommandBehavior.SchemaOnly Or CommandBehavior.KeyInfo)
      Dim dta As DataTable = dra.GetSchemaTable()
      Dim myrow As DataRow
      For Each myrow In dta.Rows
        Debug.WriteLine("Column Name:  " + myrow(0).ToString)
        dim htFields As New Hashtable()
        htFields.Add(myrow(0).ToString(),false)
      Next
      htTables(str) = htFields

      htFields = Nothing
      dra.Close
      conn.Close
    Next
0
Comment
Question by:knowlton
2 Comments
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 500 total points
ID: 17991978
because you declare htFields inside the inner loop, it will only be accessible during that iteration, ie once out of the for each myrow loop the object htFields is not accessible

if you declare the htfields object outside the loop, you can use it inside the for each myrow loop and it will still exist outside that loop as well.

eg
dim htFields As New Hashtable()
 For Each myrow In dta.Rows
        Debug.WriteLine("Column Name:  " + myrow(0).ToString)
                htFields.Add(myrow(0).ToString(),false)
      Next
 htTables(str) = htFields

0
 
LVL 5

Author Comment

by:knowlton
ID: 17992107
That was it, thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now