Hashtables and Structs not updating poperly

  • Thread starter Greg Oetker via .NET 247
  • Start date
G

Greg Oetker via .NET 247

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.
 
M

Michael Culley

Greg Oetker via .NET 247 said:
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; //<------------------------------------------------------
 
H

Hin

Please note that struct is a value-type instead of refernce type

--
Regards,
Hin

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.
 
G

Greg Oetker

Thanks,

I added: Test[x] = TempSetValue; and it's working fine.


(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.


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; //<------------------------------------------------------
 
Top