Problem with Hashtable

G

Guest

Hi

i am using following code which extracts information from XML file and creates an instance of class which it adds to hash table. problem is i am unable to extract information from hashtable : here is the piece of code

public void readEmailRecords()
XmlTextReader reader = new XmlTextReader("C:\\Mailer\\EmailRec.xml")
reader.MoveToElement();


try
while(reader.Read())
if(reader.HasAttributes && reader.NodeType==XmlNodeType.Element

reader.MoveToAttribute("Subject")
if( reader.Value.Trim() != "" || reader.Value.Trim() != null
pm.setSubject(reader.Value.Trim())

reader.MoveToAttribute("From")
if( reader.Value.Trim() != "" || reader.Value.Trim() != null
pm.setFrom(reader.Value.Trim())

reader.MoveToAttribute("To")
if( reader.Value.Trim() != "" || reader.Value.Trim() != null
pm.setTO(reader.Value.Trim())

reader.MoveToAttribute("CC")
if( reader.Value.Trim() != "" || reader.Value.Trim() != null
pm.setCC(reader.Value.Trim())

reader.MoveToAttribute("BCC")
if( reader.Value.Trim() != "" || reader.Value.Trim() != null
pm.setBCC(reader.Value.Trim())

reader.MoveToAttribute("DateTime")
if( reader.Value.Trim() != "" || reader.Value.Trim() != null
pm.setDateTime(reader.Value.Trim())

reader.MoveToAttribute("Body")
if( reader.Value.Trim() != "" || reader.Value.Trim() != null
pm.setBody(reader.Value.Trim())



if(!htMessages.ContainsKey(pm.GetHashCode())

htMessages.Add(pm.GetHashCode(), pm);

}
reader.Close()
reader=null

}
catch(Exception e) { MessageBox.Show(null,"XML Exception : " + e.ToString(),"Error");


private void frmMailer_Load(object sender, System.EventArgs e

pm = new PopMessage()
htMessages = new Hashtable()
readEmailRecords();

foreach(string c in htMessages.Keys
Console.WriteLine( ( (PopMessage) htMessages[c]).getFrom() )




Instead of printing value from getFrom(), it just prints a blank strin

NOTE : pm & htMessges are defined as a class member variable

Thanx in advanc
 
N

Nicholas Paldino [.NET/C# MVP]

Ronin,

You are adding using the hashcode returned by GetHashCode on the object
as the key, which is an integer, which has its own hashcode different from
the one your object generated. You should use the identifier of pm
(something like a string that identifies it, or a number) as the key, and
let the Hashtable get the hash itself.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronin said:
Hi,

i am using following code which extracts information from XML file and
creates an instance of class which it adds to hash table. problem is i am
unable to extract information from hashtable : here is the piece of code :
public void readEmailRecords() {
XmlTextReader reader = new XmlTextReader("C:\\Mailer\\EmailRec.xml");
reader.MoveToElement();


try {
while(reader.Read()) {
if(reader.HasAttributes && reader.NodeType==XmlNodeType.Element)
{
reader.MoveToAttribute("Subject");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setSubject(reader.Value.Trim());

reader.MoveToAttribute("From");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setFrom(reader.Value.Trim());

reader.MoveToAttribute("To");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setTO(reader.Value.Trim());

reader.MoveToAttribute("CC");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setCC(reader.Value.Trim());

reader.MoveToAttribute("BCC");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setBCC(reader.Value.Trim());

reader.MoveToAttribute("DateTime");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setDateTime(reader.Value.Trim());

reader.MoveToAttribute("Body");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setBody(reader.Value.Trim());
}


if(!htMessages.ContainsKey(pm.GetHashCode()))
{
htMessages.Add(pm.GetHashCode(), pm);
}
}
reader.Close();
reader=null;

}
catch(Exception e) { MessageBox.Show(null,"XML Exception : " + e.ToString(),"Error");}
}

private void frmMailer_Load(object sender, System.EventArgs e)
{
pm = new PopMessage();
htMessages = new Hashtable();
readEmailRecords();

foreach(string c in htMessages.Keys)
Console.WriteLine( ( (PopMessage) htMessages[c]).getFrom() );


}

Instead of printing value from getFrom(), it just prints a blank string

NOTE : pm & htMessges are defined as a class member variables

Thanx in advance
 
N

Nicholas Paldino [.NET/C# MVP]

Ronin,

You shouldn't be generating the hashcode at all. The Hashtable is
responsible for that. You should use as the key whatever value makes sense.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronin said:
Hi,

thanx for replying... i am using following code to generate hashcode

public string GetHashCode()
{
return mFrom+mSubject+mTo;
}

i am assuming that this will generate a unique hash code



----- Nicholas Paldino [.NET/C# MVP] wrote: -----

Ronin,

You are adding using the hashcode returned by GetHashCode on the object
as the key, which is an integer, which has its own hashcode different from
the one your object generated. You should use the identifier of pm
(something like a string that identifies it, or a number) as the key, and
let the Hashtable get the hash itself.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronin said:
and
creates an instance of class which it adds to hash table. problem is i am
unable to extract information from hashtable : here is the piece of code :
public void readEmailRecords() {
XmlTextReader reader = new XmlTextReader("C:\\Mailer\\EmailRec.xml");
reader.MoveToElement();
while(reader.Read()) {
if(reader.HasAttributes && reader.NodeType==XmlNodeType.Element)
{
reader.MoveToAttribute("Subject");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setSubject(reader.Value.Trim());
reader.MoveToAttribute("From");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setFrom(reader.Value.Trim());
reader.MoveToAttribute("To");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setTO(reader.Value.Trim());
reader.MoveToAttribute("CC");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setCC(reader.Value.Trim());
reader.MoveToAttribute("BCC");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setBCC(reader.Value.Trim());
reader.MoveToAttribute("DateTime");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setDateTime(reader.Value.Trim());
reader.MoveToAttribute("Body");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setBody(reader.Value.Trim());
}
if(!htMessages.ContainsKey(pm.GetHashCode()))
{
htMessages.Add(pm.GetHashCode(), pm);
}
}
reader.Close();
reader=null;
catch(Exception e) { MessageBox.Show(null,"XML Exception : " + e.ToString(),"Error");}
}
private void frmMailer_Load(object sender, System.EventArgs e)
{
pm = new PopMessage();
htMessages = new Hashtable();
readEmailRecords();
foreach(string c in htMessages.Keys)
Console.WriteLine( ( (PopMessage) htMessages[c]).getFrom() );
}
Instead of printing value from getFrom(), it just prints a blank string
NOTE : pm & htMessges are defined as a class member variables
Thanx in advance
 
G

Gang Peng

Your GetHashCode function will not be used at all.
Note the one defined in Object is "int GetHashCode", which is also the one
you need to override.
Gang Peng
[MSFT]

Ronin said:
Hi,

thanx for replying... i am using following code to generate hashcode

public string GetHashCode()
{
return mFrom+mSubject+mTo;
}

i am assuming that this will generate a unique hash code



----- Nicholas Paldino [.NET/C# MVP] wrote: -----

Ronin,

You are adding using the hashcode returned by GetHashCode on the object
as the key, which is an integer, which has its own hashcode different from
the one your object generated. You should use the identifier of pm
(something like a string that identifies it, or a number) as the key, and
let the Hashtable get the hash itself.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronin said:
and
creates an instance of class which it adds to hash table. problem is i am
unable to extract information from hashtable : here is the piece of code :
public void readEmailRecords() {
XmlTextReader reader = new XmlTextReader("C:\\Mailer\\EmailRec.xml");
reader.MoveToElement();
while(reader.Read()) {
if(reader.HasAttributes && reader.NodeType==XmlNodeType.Element)
{
reader.MoveToAttribute("Subject");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setSubject(reader.Value.Trim());
reader.MoveToAttribute("From");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setFrom(reader.Value.Trim());
reader.MoveToAttribute("To");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setTO(reader.Value.Trim());
reader.MoveToAttribute("CC");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setCC(reader.Value.Trim());
reader.MoveToAttribute("BCC");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setBCC(reader.Value.Trim());
reader.MoveToAttribute("DateTime");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setDateTime(reader.Value.Trim());
reader.MoveToAttribute("Body");
if( reader.Value.Trim() != "" || reader.Value.Trim() != null)
pm.setBody(reader.Value.Trim());
}
if(!htMessages.ContainsKey(pm.GetHashCode()))
{
htMessages.Add(pm.GetHashCode(), pm);
}
}
reader.Close();
reader=null;
catch(Exception e) { MessageBox.Show(null,"XML Exception : " + e.ToString(),"Error");}
}
private void frmMailer_Load(object sender, System.EventArgs e)
{
pm = new PopMessage();
htMessages = new Hashtable();
readEmailRecords();
foreach(string c in htMessages.Keys)
Console.WriteLine( ( (PopMessage) htMessages[c]).getFrom() );
}
Instead of printing value from getFrom(), it just prints a blank string
NOTE : pm & htMessges are defined as a class member variables
Thanx in advance
 
Top