1 Reply Latest reply on Nov 24, 2010 10:51 AM by Flex harUI

    Object initializer syntax for strongly typed objects

    JoshBeall Level 1

      Hi All,


      In C#, you can initialize an object like this:


      StudentName student = new StudentName {FirstName = "John", LastName = "Smith"};


      I know that in ActionScript, I can create an object without an explicit type declaration using a similar syntax, like this:


      var student:Object = {FirstName:"John", LastName:"Smith" };


      But what if I wanted to strongly type the object I'm creating, like this:


      var student:Student = {FirstName:"John", LastName:"Smith" };


      That will fail with a compile error because I'm trying to convert a standard Object instance to a Student object instance.


      Does ActionScript have a C#-like initializer syntax?  Essentially, it gives you initialization constructors without you having to do the busy-work writing them.  And, as an added bonus, you can initialize only the properties you want to, and you don't have to explicitly pass null for the properties you don't want to initialize.