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;
}
}
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;
}
}