Given two strings, say hashKey and hashVal, I add the pair to a hash object. In this example hashVal is a string representing an integer, and thus I parse it as such before storing it in the table.
Now here's the problem. The value stored in the hash table is actually an int32 object, which makes it troublesome to use later inside expressions. After much time looking, I have been unable to find an easy way to either store an actual int or extract the value stored as an int instead of an int32 object.
Below is an example of what I'm trying to do:
var myHash : HashObject;
var intTemp : int;
var hashKey : String;
var hashVal : String;
hashKey = "foobar";
hashVal = "123";
if(System.Int32.TryParse(hashVal,intTemp))
{
intTemp = int.Parse(hashVal);
myHash.Add(hashKey,hashVal);
}
// later, attempt to retrieve and use the value:
var someMath : int;
someMath = 456 + myHash["foobar"];
this generates a compile time error:
BCE0051: Operator '+' cannot be used with a left hand side of type 'int' and a right hand side of type 'Object'.
If I try to cast the object, I instead get the runtime error:
InvalidCastException: Cannot cast from source type to destination type.
I know that I can first store the value retrieved inside a new int before using it, but that would be a very lengthy and inelegant solution for the quantity of math and number of key-value pairs I'll be using and thus mostly negate the benefits of using a hash table in the first place.
Any ideas?