# Opinion sought on Explicit Type Conversion

**URL:** <https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236>\
**Category:** flash\
**Created:** [March 24, 2006, 9:37pm UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236 "2006-03-24T21:37:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soulberry](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@Soulberry](https://forum.kirupa.com/u/Soulberry)\
**Post date:** [March 24, 2006, 9:37pm UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/1 "2006-03-24T21:37:53Z")

</div>

I am a novice and am educating myself about AS.

Reading through Datatypes and conversion, I came across different methods to converting data into “string”.

1. by using the toString() method
2. by using the String() function
3. converting to a string using empty string concatenation :- x + “”;

My questions are :  
a) which of the three is the preferable method?  
b)Why?  
c) Are there any specific instances when one is preferred over the other?  
d) if so, would anybody be kind enough to illustrate it to me.

I seek your wisdom on this matter since I cannot help wonder whay there are so many different methods to achieve the same result.

I thank you all.

P.S. My textbooks are Mr. Colin Moock’s two books and I cannot find the explanation in there…

---

<div class="post-metadata">

**Author:** ![v2ikematu](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@v2ikematu](https://forum.kirupa.com/u/v2ikematu)\
**Post date:** [March 24, 2006, 10:23pm UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/2 "2006-03-24T22:23:53Z")

</div>

Moock’s books are thre best. I prefer String()… maybe because I also use other conversions that way. by just casting them. 🙂 seems pretty logical to me.

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [March 24, 2006, 10:27pm UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/3 "2006-03-24T22:27:12Z")

</div>

I think the preferable way to do it is the cast it is to declare it as a string when creating the variable like this:

```auto
var someVar:String = "some value"

```

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 25, 2006, 12:34am UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/4 "2006-03-25T00:34:00Z")

</div>

> [@Defective](#):
>
> I think the preferable way to do it is the cast it is to declare it as a string when creating the variable like this:
> 
> ActionScript Code:  
> [FONT=Courier New][LEFT][COLOR=#000000] **var** [/COLOR] someVar:[COLOR=#0000ff]String[/COLOR] = [COLOR=#ff0000]“some value”[/COLOR];  
> [/LEFT]  
> [/FONT]

That won’t covert the data to the correct type but rather have the compiler explode in your face when you try and reference data of an incorrect type (which, don’t get me wrong, isn’t a bad thing).

_toString_ is given to all objects automatically. Calls to the _String_ function (type casting) and _String.concat_ (with non-String objects) simply call the \*toString \*method of the instance. All in all, the three different ways are exactly the same in that the all ultimately call the _toString_ method.

The only important thing to note is that the _toString_ method can be overwritten:  
[LIST=1]  
[_]In the prototype of the class governing the object which will affect all instances of that class.  
[_]As a property directly of the instance, which will only affect that instance.[/LIST]Thus modifying the return values of the _String_ function and\* String.concat\*.

:sc:

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [March 25, 2006, 12:41am UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/5 "2006-03-25T00:41:06Z")

</div>

i use the declaring method and it usually works for me. Like this:

```auto
var stringVar:String = "4";
var numberVar:Number = stringVar;
trace (numberVar+4); //traces 8 i believe

```

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 25, 2006, 12:46am UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/6 "2006-03-25T00:46:34Z")

</div>

That is where the compiler explodes in your face 😉

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [March 25, 2006, 12:48am UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/7 "2006-03-25T00:48:18Z")

</div>

:D…i dont rememeber it exploding on me.

---

<div class="post-metadata">

**Author:** ![Soulberry](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@Soulberry](https://forum.kirupa.com/u/Soulberry)\
**Post date:** [March 25, 2006, 7:42pm UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/8 "2006-03-25T19:42:19Z")

</div>

> [@Defective](#):
>
> i use the declaring method and it usually works for me. Like this:
> 
> ActionScript Code:  
> [FONT=Courier New][LEFT][COLOR=#000000] **var** [/COLOR] stringVar:[COLOR=#0000ff]String[/COLOR] = [COLOR=#ff0000]“4”[/COLOR];\</p\>  
> \<p\>var numberVar:[COLOR=#0000ff]Number[/COLOR] = stringVar;\</p\>  
> \<p\>trace COLOR=#000000[/COLOR]; [COLOR=#808080]\*//traces 8 i believe \*[/COLOR]
> 
> [/LEFT]  
> [/FONT]

Thanks V2ikematu - yes Moock’s are interestingly written, TheCanadian and Defective for sharing your thoughts.

When I tried the above code, the trace() command returned the following error:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: Type mismatch in assignment statement: found String where Number is required.  
var numberVar:Number = stringVar;  
Total ActionScript Errors: 1 Reported Errors: 1

Why is that happenning? I’m on MX 2004 pro

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 25, 2006, 7:50pm UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/9 "2006-03-25T19:50:25Z")

</div>

> [@Soulberry](#):
>
> Thanks V2ikematu - yes Moock’s are interestingly written, TheCanadian and Defective for sharing your thoughts.
> 
> When I tried the above code, the trace() command returned the following error:
> 
> **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: Type mismatch in assignment statement: found String where Number is required.  
> var numberVar:Number = stringVar;  
> Total ActionScript Errors: 1 Reported Errors: 1
> 
> Why is that happenning? I’m on MX 2004 pro

That’s what I mean by compiler exploding in your face. Like I said, giving variables a strict data type won’t convert data to the correct type but rather tell you when you try and reference the wrong type - by giving you an error - and that is where you use type casting to make it the correct type 🙂

---

<div class="post-metadata">

**Author:** ![Soulberry](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@Soulberry](https://forum.kirupa.com/u/Soulberry)\
**Post date:** [March 25, 2006, 10:11pm UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/10 "2006-03-25T22:11:09Z")

</div>

> [@TheCanadian](#):
>
> That’s what I mean by compiler exploding in your face. Like I said, giving variables a strict data type won’t convert data to the correct type but rather tell you when you try and reference the wrong type - by giving you an error - and that is where you use type casting to make it the correct type 🙂

Yes, I am beginning to understand how the interpreter works now. Since I last posted here, I have read on and found that Mr. Moock touches upon the subject once again in later chapters.

toString() is more flexible. I have tried out various combinations and I am getting the picture gradually, as they say.

Thank you TheCanadian. Let me also add that I have seen some of your creations posted on the web and have appreciated them very much.

Regards  
soul

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 25, 2006, 10:41pm UTC](https://forum.kirupa.com/t/opinion-sought-on-explicit-type-conversion/183236/11 "2006-03-25T22:41:57Z")

</div>

Glad to help :mountie:
