This problem should be easy. I have a Hashtable of
objects - "book" . Each "book" or object has a field -
"customer" and a hashTable of "contentObj" -
where "content" contains several fields. Later in my
program I need to pull the book objects out. My intent is
to have the user interface simply call when needed
a 'movenext' function.
Unfortunately I can't seem to convert the objects pulled
out by the enum of the hash to the book object. In the
locals window I cam see the key to my book object but the
count reflects the count of the content Objects. Can
anyone point out what/why I should do to fix this...
Thanks - console app below can be pasted into test project
using System;
using System.Collections;
using System.Xml;
namespace enumHashTable
{
public class bookObj
{
private string m_customer="";
private Hashtable IntManualhash= new
Hashtable(); // for all the bookObjects
private
System.Collections.IDictionaryEnumerator bookEnum;
private System.Collections.IDictionaryEnumerator
contentEnum;
public void bookReset()
{
bookEnum =
IntManualhash.GetEnumerator();
bookEnum.Reset();
}
public bookObj()
{
m_customer="";
}
public void addtohash(object
theKey,contentObj contentObj)
{
string contentKey=
contentObj.label;
if ( ! IntManualhash.ContainsKey
(theKey))
{
// we should then create a
new Hash for the content(s)...
Hashtable Intcontenthash =
new Hashtable();
Intcontenthash.Add(theKey,
contentObj);
IntManualhash.Add
(theKey,Intcontenthash);
}
else // locate correct item..and
add the contentObject
{
Hashtable
IntExistcontenthash=(Hashtable)IntManualhash[theKey];
IntExistcontenthash.Add
(contentKey,contentObj);
//Console.WriteLine
(IntExistcontenthash.Count);
}
}
public void testHashExtract()
{
bookEnum.MoveNext();
object x = bookEnum.Current; // cannot convert
//
appears bookEnum pointed at contents
bookObj thisBook = (bookObj)
bookEnum.Current;
Console.WriteLine(x.GetType());
}
public string customer
{
get {return m_customer;}
set {m_customer=value;}
}
public class contentObj
{ // turn these into properties with
get/set ?
public string label;
public int number;
public string drvltr;
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
/// new stuff testing
string bookKey;
string customer;
customer="FRED";
bookObj book = new bookObj(); // established
once for the master bookhash
book.customer=customer;
bookKey="book num 1";
bookObj.contentObj cdContent = new
bookObj.contentObj();
cdContent.label="DISK 2";
cdContent.number=2;
book.addtohash(bookKey,cdContent);
book.customer=customer;
/// contentObj cdContent = new
contentObj();
cdContent.label="DISK 1";
cdContent.number=1;
book.addtohash(bookKey,cdContent);
cdContent.label="DISK 3";
cdContent.number=3;
book.addtohash(bookKey,cdContent);
cdContent.label="DISK 4";
cdContent.number=4;
book.addtohash(bookKey,cdContent);
book.bookReset();
book.testHashExtract();
// TODO: Add code to start
application here
//
}
}
Quote:
}