473,473 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with Hashtable

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
Nov 16 '05 #1
3 2479
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]
- mv*@spam.guard.caspershouse.com

"Ronin" <fa*********@covad.net> wrote in message
news:D3**********************************@microsof t.com...
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

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Ronin" <an*******@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com

"Ronin" <fa*********@covad.net> wrote in message
news:D3**********************************@microsof t.com...
> 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
>>

Nov 16 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com

"Ronin" <fa*********@covad.net> wrote in message
news:D3**********************************@microsof t.com...
> 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
>>

Nov 16 '05 #4

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

Similar topics

0
by: Ricardo Pereira | last post by:
Hi, I wrote a simple program to test the "out-of-the-box" functionalities of the CMAB (Configuration Management Application Block). After having added 2 values to an hashtable, and having written...
1
by: Timo Qvist | last post by:
Hi, I'm a bit new to STL and really new to SGI's hash_set implementation and I've having problem instantiating a hash_set with a custom hash function, I could really use some help sifting through...
9
by: Billy Patton | last post by:
First, I'm not a student looking for help. Although there's nothing wrong with studens looking for help here. I'm learning c++ move further away from the perl wars here at work (my version is...
2
by: Marek | last post by:
I am having a problem with Hashtable used in XML web server. I have a WebMethod where clients get registered and added into hashtable. Later, I call another web method which has client ID as...
3
by: julien | last post by:
Hello, I'm sorry to send another message about the custom assemblies. The examples I find online don't help to find out what is wrong in my file. In a dll: This custom attribute: public...
1
by: Wee Bubba | last post by:
i have a class. within the constructor for this class it tests if 2 session objects exist and if they dont it creates them. this is a one off deal. The session objects are both Hashtables. ...
0
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a...
6
by: stoogots2 | last post by:
am using Visual Studio 2003, .Net Framework 1.1, C#. I get a SystemNullReferenceException when trying to do a hashtable.add(string,string) from a login page, and I do not understand why because all...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.