473,803 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)Te st[x];

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

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

When you check the TempTest.Else.T est it is set to null since thevalue wasn't set when it was created even tho we set it. But ifyou check TempSetValue.El se.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)T est[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>vVzdX369kk6 jkeTuQ+/nAQ==</Id>
Nov 16 '05 #1
3 1622
"Greg Oetker via .NET 247" <an*******@dotn et247.com> wrote in message news:ey******** ******@TK2MSFTN GP12.phx.gbl...
So it appears that when I excute the DoSomething TempSetValue = (DoSomething)Te st[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.Locati on.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)Te st[x];

// Update the values of the struct
TempSetValue.El se.Test = "Test " + x.ToString();
TempSetValue.El se.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*******@dotn et247.com> wrote in message
news:ey******** ******@TK2MSFTN GP12.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)Te st[x];

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

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

When you check the TempTest.Else.T est 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.El se.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)Te st[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>vVzdX369kk6 jkeTuQ+/nAQ==</Id>
Nov 16 '05 #3
Thanks,

I added: Test[x] = TempSetValue; and it's working fine.
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:Od******** ******@TK2MSFTN GP11.phx.gbl...
"Greg Oetker via .NET 247" <an*******@dotn et247.com> wrote in message news:ey******** ******@TK2MSFTN GP12.phx.gbl...
So it appears that when I excute the DoSomething TempSetValue =

(DoSomething)Te st[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.Locati on.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)Te st[x];

// Update the values of the struct
TempSetValue.El se.Test = "Test " + x.ToString();
TempSetValue.El se.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
2852
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
2289
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 = uri1.GetHashCode(); int h2 = uri2.GetHashCode(); bool e = uri1.Equals(uri2); if (e == true && h1 != h2)
13
1785
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 hashtable that supports (or implements) generics? I haven't tried generics hands-on, but I would assume that once you don't have to do casting anymore, things could speed up quite a bit.
4
8357
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 so on (I have complete control of the objects stored). I am currently using a set of abstract cache classes that my model managers subclass. The hashtable in a model managers could, potentially, store more than 25.000 objects (let's imagine...
1
1514
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 very simplified example of problem with the following code: Dim cust As New Hashtable Dim invoice As New Hashtable Dim invoiceLines As New Hashtable
2
3154
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 need to write to it, but now it occurred to me that maybe I could just lock one of the Hashtables inside without locking the entire object, but then I thought maybe some thread could instruct the outside Hashtable to remove an inside Hashtable...
14
1798
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 CPU per second. I've gone from a single-core/single-proc, to a dual-core/single-proc to a dual-core/dual-proc and as I suspected the number of requests per second per CPU does not increase it remains at 5 per CPU.
7
2680
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 foreach loop I retreive the corresponding hashtable from the masterArrayList and add new strings to existing_strings after comparing the hashtable keys. The problem is that when I add a new value to an existing_strings arraylist it also add the new...
7
1976
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 that data (e.g. IDs 2,4). What I want to do is to iterate through Hashtable 1 (using foreach?) checking if each ID/Description is in Hashtable 2, and if not, then I want to add the ID/Description into Hashtable 3. So by the end of this I will...
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9565
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5501
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.