Hashtable behavior?

A

A. Burch

I'm wanting to use a Hashtable. I have 2 (keys, objectvalues) pairs of data
to store. I'm using a static declartion for the Hashtable the instance of
the class that is the object. If I try and retreive the first object after
storing after the 2 (key, objectvalues) are stored I always get the last
object value stored. Why doesn't qrec = (Q)HT["First"] give a copy of the
last or in this code the second of the 2 objects? How should the code be
done to get a copy of the first object?

using System;
using System.Collections;
using System.Data;
using System.Messaging;

public class SamplesHashtable
{
static Hashtable HT = new Hashtable();
Q qrec = new Q();
double x = new double();

public static void Main()
{
// Creates and initializes a new Hashtable.
qrec.symbol = "HI";
qrec.price = 4.0;
HT.Add("First", qrec);

qrec.symbol = "BYE";
qrec.price = 5.0;
HT.Add("Second", qrec);

qrec = (Q)HT["First"]; // This won't get the first
object value, but has the
// second object
value.
qrec.symbol = "MONDAY";
qrec.price = qrec.price - 1.0; //
HT["First"] = qrec;
}

[System.Xml.Serialization.XmlRoot("Q")]
public class Q
{
public string symbol;
public double price;
}
}
 
A

AlexS

Hi A. Burch

it's because second assignment to qrec properties overwrites values, which
were assigned initially. So when you get first element back from table it
contains changed data. Effectively you add same object twice changing its
content in between.
Problem is with your code, not with Hashtable. If you want to keep 2
different quotes in table you have to use code like this:
Q q1 = new Q();
<assign values to q1 and add it to table>
Q q2 = new Q();
<assign values to q2 and add it to table>
....
Hope this clears the situation for you
Alex
 
G

Greg Young

its because qrec is a reference type ... thus only a reference (pointer if
its easier to think about) is kept in the hash table. What happens is both
of the entries point to the same object ... so when you make changes to that
object it affects both. You can get around this either by implementing a
deep copy (Clone method) or by ...
qrec = new Q();
qrec.symbol = "BYE";
qrec.price = 5.0;
HT.Add("Second", qrec);


using System;
using System.Collections;
using System.Data;
using System.Messaging;

public class SamplesHashtable
{
static Hashtable HT = new Hashtable();
Q qrec = new Q();
double x = new double();

public static void Main()
{
// Creates and initializes a new Hashtable.
qrec.symbol = "HI";
qrec.price = 4.0;
HT.Add("First", qrec);
qrec.symbol = "BYE";
qrec.price = 5.0;
HT.Add("Second", qrec);

qrec = (Q)HT["First"]; // This won't get the first
object value, but has the
// second object
value.
qrec.symbol = "MONDAY";
qrec.price = qrec.price - 1.0; //
HT["First"] = qrec;
}

[System.Xml.Serialization.XmlRoot("Q")]
public class Q
{
public string symbol;
public double price;
}
 
M

mikeb

A. Burch said:
I'm wanting to use a Hashtable. I have 2 (keys, objectvalues) pairs of data
to store. I'm using a static declartion for the Hashtable the instance of
the class that is the object. If I try and retreive the first object after
storing after the 2 (key, objectvalues) are stored I always get the last
object value stored. Why doesn't qrec = (Q)HT["First"] give a copy of the
last or in this code the second of the 2 objects? How should the code be
done to get a copy of the first object?

using System;
using System.Collections;
using System.Data;
using System.Messaging;

public class SamplesHashtable
{
static Hashtable HT = new Hashtable();
Q qrec = new Q();
double x = new double();

public static void Main()
{
// Creates and initializes a new Hashtable.
qrec.symbol = "HI";
qrec.price = 4.0;
HT.Add("First", qrec);

qrec.symbol = "BYE";
qrec.price = 5.0;
HT.Add("Second", qrec);

qrec = (Q)HT["First"]; // This won't get the first
object value, but has the
// second object
value.
qrec.symbol = "MONDAY";
qrec.price = qrec.price - 1.0; //
HT["First"] = qrec;
}

[System.Xml.Serialization.XmlRoot("Q")]
public class Q
{
public string symbol;
public double price;
}
}

Wuite simply, you are not adding 2 objects to the hashtable. You add an
object with "First" as the key, then you change that same object, and
re-add it with "second" as the key. So now you have 2 hashtable entries
pointing to the same object.

The fix is to simply create new objects to add to the table:


// Creates and initializes a new Hashtable.
qrec = new Q();
qrec.symbol = "HI";
qrec.price = 4.0;
HT.Add("First", qrec);

qrec = new Q();
qrec.symbol = "BYE";
qrec.price = 5.0;
HT.Add("Second", qrec);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top