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>