dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: variable name to string

  1. #1
    Senior Member
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    326

    variable name to string

    Is it possible to convert a given variable name to a string?

    Code:
    function vartostring(mystring:String) {
        //something like var newstring:String = parsevarname(mystring)
        trace (newstring);//output 'hello'
    }
    vartostring(hello);

    Illustration | Animation | Web Banners | Graphic Design
    Ducklord Studio

  2. #2
    Senior Member
    Join Date
    Jan 2004
    Posts
    141
    Quote Originally Posted by digitalecartoon View Post
    Is it possible to convert a given variable name to a string?

    Code:
    function vartostring(mystring:String) {
        //something like var newstring:String = parsevarname(mystring)
        trace (newstring);//output 'hello'
    }
    vartostring(hello);
    Maybe it's late in the day, but this doesnt make sense to me. If you know the name of the variable (hello) because you're passing it in as an argument, why do you need to try and retrieve the name again? Are you trying to do something like this? (see below) Maybe I just need to catch up on some sleep.

    Code:
    var test:String = "example"
    trace(printVarName("test"))
    
    function printVarName(varname:String):String
    {
    	return(this[varname])
    }

  3. #3
    Senior Member
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    326
    Well, I came up with this question working on this: I wanna write a function where I can pass the hexadeximal colour number e.g.

    Code:
    function colorme(CCADFF);
    And the function 'colorme' being something like:

    Code:
    function colorme(mystring:String) {
        trace ("0x"+mystring);
    }
    To form a 0xCCAD00 number which I can pass to a colortransform function.

    But CCADFF would now be seen as a variable off course and transfer as 'undefined'.

    I would have to add quotes

    Code:
    function colorme("CCADFF");
    or the complete hex number

    Code:
    function colorme(0xCCADFF);
    But to make it simple I would just want to pass the name

    Code:
    function colorme(CCADFF);
    That's why I was looking for a function which would prevent this to be seen as a variable and instead turn it into a string.

    Possible or not?

    Illustration | Animation | Web Banners | Graphic Design
    Ducklord Studio

  4. #4
    Senior Member
    Join Date
    Jan 2004
    Posts
    141
    errrm, why not just pass it in as a string? I know what you're getting at, but a) I dont think it's possible and b) I don't see the advantage.

    Code:
    colorMe("ff9933")
    
    function colorMe(colorAsHex:String):Void
    {
    	myColor = new Color(myMovieClip);
    	myColor.setRGB("0x" + colorAsHex);
    }
    or assign the color to a variable first, and pass in the variable:

    Code:
    var myColor:String 
    myColor = "0xff9933"
    colorMe(myColor)
    
    function colorMe(colorAsHex:String):Void
    {
    	myColor = new Color(myMovieClip);
    	myColor.setRGB(colorAsHex);
    }
    I've not tested this code because I'm not near my dev. machine, but you get the idea....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center