Hey guys,
I’m pulling my hair out on this one:
I have a Hashtable holding some data and need to cast those objects back to use them. I simply can’t figure out how to do this.
Simple example:
var ht:Hashtable = new Hashtable({"score" : 123546, "name" : "hellslawyer"});
var td:int = ht["0"];
Throws a casting error when pragma strict is set. “parseInt” or “as int” throw errors as well. What the heck do I have to do, to cast the value to the correct type to be able to work with it later on?
Thanks in advance!
hellslawyer
You need to cast the object you get back from ht to an int yourself, with pragma strict there is no type casting automagic anymore taking away these vital steps
Yes, I knew that already, but HOW do I cast it back when neither “parseInt” nor “as int” nor “new int(ht[“0”])” works?
The only thing that works is this workaround:
var ht:Hashtable = new Hashtable({"score" : 123546, "name" : "hellslawyer"});
var td:int = parseInt(ht["score"].ToString());
But that can’t be the final solution.
as XXX only works for ref types ie no numbers.
int.Parse(…) should work normally with a string on the other side thats an actual number
Sure thing, that’s why my workaround helps. But I simply can’t understand why I have to cast the object to a string first to then cast it back to its original type. ht[“score”] is definitely an int. Even typeof(ht[“score”]) tells my that. So there should be no need to cast it twice. Or am I missing something here?
Pretty simple reason you need to cast:
-
HashTable only stores objects, anything you throw in becomes Object (System.Object to be precise, not UnityEngine.Object). numbers will get boxed to fullfill this as they are no classes and don’t extend from it
-
As such ht[“score”] is an object (a boxed int) too.
Its new to me that typeof even works on UnityScript, but generally, there are sideeffects of using it where the answer you get will tell you what would be in in the end after “automagic” but you won’t get there without doing it manually under pragma strict
-
The cast to string is required cause there is simply no function to parse int from object, at least I don’t know any
Thanks dreamora,
that pretty much accomplished, that I’ve understood how it works and how I have to cast.
Anyways, it’s kind of strange to cast twice when coming from something like AS3 where you can create an object and the valuetypes are stored within the object’s values. Which also seems to be the case in System.Object, beside you can’t cast it back.
It might look strange but thats because an object can be anything, but only a handfull of classes of this anything can contain a number at all, that are basically other numbers and strings. object can be millions of other things than these few classes and for all those there is no way to cast it (and wouldnt make sense as the int then normally would be field on the object)
Technically, at least on C#, you can omit the ToString() cause ToString is done automatically if a function expects a string, but I guess pragma strict will be strict in this case too.
in AS3 the whole thing is differently as its a special case as its not compiled which enables it to do some dirty things that on native compiles is impossible or very time intense. Pragma strict gives you an idea on the level of restrictions and that although its technically only required for 4 platforms (but it helps a lot to identify issues everywhere), which are iOS and all 3 consoles (generally any platform where AoT compilation is used, not JIT)
Thanks for the clarification, dreamora!