- Home /
Typing a value, not a var, syntax in js?
Hi to all! Silly sintax problem: I need to pass a UInt16 value to a function. How can I do it without defining a UInt16 var before?
For the moment I have to do this :
var one : UInt16 = 1;
var oneUInt16Bytes : Byte[] = BitConverter.GetBytes(one);
Many thanks!
Answer by Bunny83 · Jan 18, 2012 at 12:36 PM
I think the only way in UnityScript is to use a temp variable since UnityScript doesn't have a value-type cast operator (only the as-cast which only works for reference types).
Implicit casting doesn't help since the BitConverter have an overloaded function for every type. To enforce a uint16 parameter you need to pass a variable of that type.
ps. It would be better when you include the line of code you're trying to get to work. Feel free to edit your question.
Thanks a bunch, I was afraid of posting code that looked silly because I was declaring a variable simply to type it before passing it to BitConverter. Looks like I was using the only way! I should have been more clear in my question, sorry. Now edited.
Answer by Kryptos · Jan 12, 2012 at 10:31 PM
Well just do:
MyFunction( 3 );
If the signature of MyFunction is
MyFunction( UInt16 val );
Then it will be implicitly converted (I guess, since it is so in C#). If it is not the case (compilation or runtime raising a warning or an error), then cast it:
MyFunction( (UInt16) 3 );
Sorry, I should have been more precise: I am using BitConverter.GetBytes(), which doesn't know the type you're passing to it...