Notifications
Clear all

[Closed] dotnet Hashtable: modifying values after adding them

Just had my Eureka! moment while looking for a way to modify existing values in a dotnet Hashtable ( dotnetobject “System.Collections.Hashtable” ). So I thought i’ll share my findings

As maxscripters may be aware, one can make use of the dotNet Hashtable class for Key/Value pair like datastorage. Like in the following simple example

ht = dotnetobject “System.Collections.Hashtable”

ht.Add “FirstEntry” 10
ht.Add “SecondEntry” 20
ht.Add “ThirdEntry” 30

print ht.count
– prints 3

print ht.Item[“FirstEntry”]
– prints 10

print ht.Item[“ThirdEntry”]
– prints 30

Now the problem i had, was CHANGING the values of already EXISTING keys, once they were added to the hashtable. Things like the following do not work for some reason ( i guess sloppy implementation )

ht.Item[“SecondEntry”] = 80
– spits out “– Runtime error: No method found which matched argument list”

The only solution directly available seemed to be REMOVING the existing key and adding it again with the new value, which seemed pretty bad to me. So after an hour fiddling arround and looking for alternatives i finally found out, that the hashtable object exposes it’s Item property setter/getter functions, despite them not appearing in the showmethods() output ( thus my initial trouble knowing about them )

set_Item()
get_Item()

So to change the value for existing Keys ( or create it if it’s not already in the hashtable) , one can simply use the following code and everything is fine

ht.set_Item “SecondEntry” 80

Now for another thing:
When strings get used as hashtable keys, those are handled in a CASE SENSITIVE manner
So ht.Item[“SecondEntry”] resolves to a different hashtable entry than ht.Item[“secondEntry”]

To create a case-ignoring Hashtable, which would not seperate those two key strings, you can create the hashtable using the following maxscript code

caseInsensitiveHT = dotNetObject “System.Collections.Hashtable” ( dotNetObject “System.Collections.CaseInsensitiveHashCodeProvider” ) ( dotNetObject “System.Collections.CaseInsensitiveComparer” )

For hashtables created this way, the following two lines would point to the same hastable entry

caseInsensitiveHT.Item[“SecondEntry”]
caseInsensitiveHT.Item[“secondEntry”]

2 Replies

This is very good info, Josef.
I recently find out that get/set_item fn’s can be used with .net hashtable.
Thanks for sharing your discovery especially for “not case-sensitive” mode.

Pretty cool, have you tried storing data as dotNetMxsValue using the Hashtable?