A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Passing var type name as parameter?

  1. #1
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341

    Passing var type name as parameter?

    Hi,

    I would like to know if is possible to pass a var type as parameter to a function. I have several sounds in library, and I would like to do something like that:

    Code:
    function PlayMe(soundname:String)
    {
    	var snd:this[soundname] = new this[soundname];
    	snd.play();
    }
    Thanks!
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, you need to pass the Class instead of a String. If for some reason you want to get a Class by name, use getDefinitionByName.

    Code:
    function playMe(soundclass:Class):void{
      var snd:Sound = new soundclass(); //assumes that soundclass extends Sound.  
      snd.play();
    }

  3. #3
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    With this, note that types (anything after the ":" in a variable declaration) cannot be dynamic. They need to be known types at compile time. Any value passed into a function as an argument or other variables have dynamic values that cannot be used as types.

    If you have no idea what the soundclass class really is (is it a Sound? Or something else?) then you would have to type your variable as a generic Object.

  4. #4
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341
    Thanks a lot 5Tons, is exactly what I was looking for!

    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  5. #5
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341
    Thanks Senocular,

    Good tip. I always fall in this pitfall, and am always trying to make a dynamics declaration after :!

    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

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