473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Storing a struct or class in a hashtable and modifying it.

Hello,

Here is some code:

I'm thinking that when storing an object in an ArrayList or Hashtable
that it clones the object. This can be shown like so:

// key is a generated hashcode for this function (its unique).

public struct Test {
int m_state;
}

p_newItem.m_state = 0; // Set the state
m_myHash.Add( (object) key, (object) p_newItem); // Store the object in
the hashtable
p_newItem.m_state = 1; // Now change the value

Test t = (Test) m_myHash[ (object) key ];

Console.WriteLine( t.m_state ); // != 1 its still 0!!
What is going on here? My only guess is that the object is stored as a
clone of the original. Not as a reference as I would expect.
Nov 16 '05 #1
7 4967
Bill... You are using a struct which has value semantics. When you pass
a value type to a method you get a copy of the value, not a copy of a
reference. When you pass a value type to a collection it gets boxed. I
would think you would get a reference to a boxed _copy_ of the value.

Regards,
Jeff
What is going on here? My only guess is that the object is stored as a

clone of the original. Not as a reference as I would expect<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
Jeff Louie wrote:
Bill... You are using a struct which has value semantics. When you pass
a value type to a method you get a copy of the value, not a copy of a
reference. When you pass a value type to a collection it gets boxed. I
would think you would get a reference to a boxed _copy_ of the value.

Regards,
Jeff
What is going on here? My only guess is that the object is stored as a


clone of the original. Not as a reference as I would expect<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


If I change it to a class ( which was the original plan ) it still has
the same behaviour. Is there anyway around it?

- Bill
Nov 16 '05 #3
Bill... Does this code make sense?

using System;

namespace TestHash
{
class Test
{
public int i=0;
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
System.Collections.Hashtable h= new System.Collections.Hashtable();
Test t= new Test();
h.Add("one",t);
Test t2= (Test)h["one"];
System.Console.WriteLine(t2.i); // output 0
t.i=1;
t2= (Test)h["one"];
System.Console.WriteLine(t2.i); // output 1
System.Console.WriteLine(t == t2); // output true
System.Console.ReadLine();
}
}
}
Regards,
Jeff
If I change it to a class ( which was the original plan ) it still has

the same behaviour. Is there anyway around it?<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Bill <bi**@REMOVEcunndev.net> wrote:
If I change it to a class ( which was the original plan ) it still has
the same behaviour. Is there anyway around it?


It shouldn't have the same behaviour.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

Here is a short simple example.
using System;
using System.Collections;

namespace Interface
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class TestApp
{
public struct Test
{
public int m_state;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

Hashtable m_myHash = new Hashtable (71);

Test p_newItem = new Test();

p_newItem.m_state = 0; // Set the state
m_myHash.Add( (object) 5, (object) p_newItem); // Store the object in
the hashtable
p_newItem.m_state = 1; // Now change the value

// t should be the exact same object as p_newItem. Not a clone.
Test t = (Test) m_myHash[ (object) 5 ];

Console.WriteLine( "Stored state is " + t.m_state ); // != 1 its
still 0!!

Console.ReadLine();
}
}
}
Jon Skeet [C# MVP] wrote:
Bill <bi**@REMOVEcunndev.net> wrote:
If I change it to a class ( which was the original plan ) it still has
the same behaviour. Is there anyway around it?

It shouldn't have the same behaviour.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Nov 16 '05 #6
Bill <bi**@REMOVEcunndev.net> wrote:
Here is a short simple example.


Your Test type is still a struct though. Change it to a class, like we
suggested, and you'll see different behaviour.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Jon Skeet [C# MVP] wrote:
Bill <bi**@REMOVEcunndev.net> wrote:
Here is a short simple example.

Your Test type is still a struct though. Change it to a class, like we
suggested, and you'll see different behaviour.

That was it. I missed the part in the .net manual that said that structs
were from System.ValueType and classes where always from Object ( and
therefore passed as references ).

Thanks for the help.

- Bill
Nov 16 '05 #8

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

Similar topics

0
by: Matt | last post by:
Hi all, Suppose I have a bunch of similar structs/classes in a format like this: struct s1{ int arg1; char arg2; }; struct s2{ char arg1;
3
by: SteveM | last post by:
This is probably an easy answer but since I am fairly new to C++ for some reason I can't see it :-( Any help would be appreciated :-) I have a class: class AClass { public:
8
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello(...
3
by: Dean L. Howen | last post by:
Hi friends, In C++, we can declare a struct and write it into file just by giving the address the strct instance (using &). for example: ///////////////////////////////// typedef struct Test...
4
by: Mario | last post by:
Hy, I have the fallowing code and I don't understand why is not working ... in a header file: typedef void* cheie; typedef void* valoare; struct _Tabel; typedef _Tabel* Tabel;
2
by: hharry | last post by:
Hello All, I have a single class windows service that includes a function that runs in the ThreadPool. The class contains a hashtable which is declared in the main thread. What is the...
3
by: jetweedy | last post by:
Hi, I'm trying to figure out how I can reassign values within an array of structures. I'm trying to write a really simple text game to learn some C++, and I can't for the life of me find...
5
by: zensunni | last post by:
Whenever I try making a pointer to a struct or class, I get this error: Cannot take the address of, get the size of, or declare a pointer to a managed type(MyStruct) Code: public...
8
by: yomama | last post by:
Hello, I am writing a program in which I take a string (specifically, first and last name with a space in between) from the user and put it into a data member of a class node. I have tried various...
6
by: castironpi | last post by:
struct.Struct lets you encode Python objects into structured memory. It accepts a format string, and optionally a buffer and offset to/from which to read/write the structure. What do you think of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.