473,396 Members | 2,092 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,396 software developers and data experts.

Hashtables and Structs not updating poperly

I have an issue where I have some structs and do to the slowsearching for a match of a unsorted Array I need to useHashtables. The issue comes into play when I want to modify thecontents of the struct once a Hashtable has been created.

Sample Code:
============

[Serializable]
struct SomethingElse
{
public string Test;
public string ItsBroke;
}

[Serializable]
struct DoSomething
{
public string Name;
public SomethingElse Else;
}

// Create a Hashtable
Hashtable Test = new Hashtable();

// Create 10 new items to the Hashtable
for (int x = 0; x < 10; x++)
{
// Create an instance of the struct
DoSomething Temp = new DoSomething();

// Set a value of the struct
Temp.Name = "Name " + x.ToString();

// Add the new instance to the Hashtable
Test.Add(x, Temp);
}

// Update the Hashtable
for (int x = 0; x < 10; x++)
{
// Get an instance of the Hashtable
DoSomething TempSetValue = (DoSomething)Test[x];

// Update the values of the struct
TempSetValue.Else.Test = "Test " + x.ToString();
TempSetValue.Else.ItsBroke= "ItsBroke" + x.ToString();
}

// Get an instance of the Hashtable
DoSomething TempTest = (DoSomething)Test[0];

When you check the TempTest.Else.Test it is set to null since thevalue wasn't set when it was created even tho we set it. But ifyou check TempSetValue.Else.Test during the update routine youwill see that it changes it for the TempSetValue instance butnot in the Master Hashtable Test.

So it appears that when I excute the DoSomething TempSetValue =(DoSomething)Test[x]; line it's actually making a copy of theinstance vs a reference to the instance like it should.

This code would work fine if I was using a Class instead of astruct but I ran into other issues doing it all via Classes.

Any help would be great since I have an Array that can have morethan 400,000 elements and doing a for loop on each element for amacth is too slow.

--------------------------------
From: Greg O.

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>vVzdX369kk6jkeTuQ+/nAQ==</Id>
Nov 16 '05 #1
3 1604
"Greg Oetker via .NET 247" <an*******@dotnet247.com> wrote in message news:ey**************@TK2MSFTNGP12.phx.gbl...
So it appears that when I excute the DoSomething TempSetValue = (DoSomething)Test[x]; line it's actually making a copy of the instance vs a reference to the instance

That is correct. Same as if you do something like SomeForm.Location.X = 10; It doesn't work because you've go a copy of the location
not a reference.
like it
should.


I wouldn't say "like it should", this is the way structs work, you chose it to work that way by chosing a struct.

To solve the problem all you need to do is put the value back into the hashtable:

DoSomething TempSetValue = (DoSomething)Test[x];

// Update the values of the struct
TempSetValue.Else.Test = "Test " + x.ToString();
TempSetValue.Else.ItsBroke= "ItsBroke" + x.ToString();

Test[x] TempSetValue; //<------------------------------------------------------
--
Michael Culley
Nov 16 '05 #2
Hin
Please note that struct is a value-type instead of refernce type

--
Regards,
Hin

"Greg Oetker via .NET 247" <an*******@dotnet247.com> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...
I have an issue where I have some structs and do to the slow searching for a
match of a unsorted Array I need to use Hashtables. The issue comes into play
when I want to modify the contents of the struct once a Hashtable has been
created.

Sample Code:
============

[Serializable]
struct SomethingElse
{
public string Test;
public string ItsBroke;
}

[Serializable]
struct DoSomething
{
public string Name;
public SomethingElse Else;
}

// Create a Hashtable
Hashtable Test = new Hashtable();

// Create 10 new items to the Hashtable
for (int x = 0; x < 10; x++)
{
// Create an instance of the struct
DoSomething Temp = new DoSomething();

// Set a value of the struct
Temp.Name = "Name " + x.ToString();

// Add the new instance to the Hashtable
Test.Add(x, Temp);
}

// Update the Hashtable
for (int x = 0; x < 10; x++)
{
// Get an instance of the Hashtable
DoSomething TempSetValue = (DoSomething)Test[x];

// Update the values of the struct
TempSetValue.Else.Test = "Test " + x.ToString();
TempSetValue.Else.ItsBroke= "ItsBroke" + x.ToString();
}

// Get an instance of the Hashtable
DoSomething TempTest = (DoSomething)Test[0];

When you check the TempTest.Else.Test it is set to null since the value wasn't
set when it was created even tho we set it. But if you check
TempSetValue.Else.Test during the update routine you will see that it changes it
for the TempSetValue instance but not in the Master Hashtable Test.

So it appears that when I excute the DoSomething TempSetValue =
(DoSomething)Test[x]; line it's actually making a copy of the instance vs a
reference to the instance like it should.

This code would work fine if I was using a Class instead of a struct but I ran
into other issues doing it all via Classes.

Any help would be great since I have an Array that can have more than 400,000
elements and doing a for loop on each element for a macth is too slow.

--------------------------------
From: Greg O.

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>vVzdX369kk6jkeTuQ+/nAQ==</Id>
Nov 16 '05 #3
Thanks,

I added: Test[x] = TempSetValue; and it's working fine.
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:Od**************@TK2MSFTNGP11.phx.gbl...
"Greg Oetker via .NET 247" <an*******@dotnet247.com> wrote in message news:ey**************@TK2MSFTNGP12.phx.gbl...
So it appears that when I excute the DoSomething TempSetValue =

(DoSomething)Test[x]; line it's actually making a copy of the instance vs a reference to the instance

That is correct. Same as if you do something like SomeForm.Location.X = 10; It doesn't work because you've go a copy of the location not a reference.
like it
should.
I wouldn't say "like it should", this is the way structs work, you chose

it to work that way by chosing a struct.
To solve the problem all you need to do is put the value back into the hashtable:
DoSomething TempSetValue = (DoSomething)Test[x];

// Update the values of the struct
TempSetValue.Else.Test = "Test " + x.ToString();
TempSetValue.Else.ItsBroke= "ItsBroke" + x.ToString();

Test[x] TempSetValue; //<------------------------------------------------------ --
Michael Culley

Nov 16 '05 #4

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

Similar topics

49
by: Christopher J. Bottaro | last post by:
I find myself doing the following very often: class Struct: pass .... blah = Struct() blah.some_field = x blah.other_field = y ....
6
by: Michael Schollmeyer | last post by:
Hello, The following code writes the text string: Uri uri1 = new Uri("http://www.here.net/aplace?param=one"); Uri uri2 = new Uri("http://www.here.net/aplace?param=two"); int h1 =...
13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
4
by: Anders Borum | last post by:
Hello! I have a list of singleton classes (model managers) that store objects internally using hashtables. Each of these classes use a single hashtable to store e.g. users, pages, elements and...
1
by: Curtis | last post by:
Does anyone know the proper method to save information to embedded hashtables. I am trying to save parent/child information to hashtables but I am not getting the correct results I have made a...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
14
by: Mark S. | last post by:
Hello, I've written a high performance web app with C# and it completely relies on static hash tables (using sync) and classes. Under real world stress this app is handling 5 get requests per...
7
by: Kamran Shafi | last post by:
Hi, I am creating an arraylist (say masterArrayList) of hashtables, where each hashtable (say table) is of the format key=string, value = arraylist of strings (say existing_strings). In a...
7
by: Mike P | last post by:
I have 2 hashtables each of which I am using to store a set of IDs and Descriptions. Hashtable 1 will have the full set of data (e.g. IDs 1,2,3,4,5) whereas Hashtable 2 will only have a subset of...
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.