-
Should/Can I strict type a custom Object's properties?
Hello all,
I need to create a custom Object that basically stores some custom parameters (properties?), which I have successfully done, and the code below works fine.
PHP Code:
myObject = new Object();
myObject.nameLabel = "New Releases";
myObject.artOrientation = "portrait";
myObject.artAmount = 5;
I find the new compiler and debugger capabilities very helpful, since I'm not the most detail-oriented, so I (usually) like that Flash forces me to strict-type my variables and such.
Anyway, I was wondering if it's a "best practice" to strict data type a custom objects' properties, and if so, how to do it in the logic above? I kept getting syntax errors when I just tried it initially.
I know if I created a custom Class .AS file, I would be forced to data type the properties... but I'd rather just do it "in-line" in my main .FLA file.
Also, I know I can just pass typed VARIABLES to the properties... but I'd rather have the properties strict-typed themselves... so if I try to pass a variable as the wrong type, then I would still get an error.
Thanks!
-
Yes, it is best practice to create a class to represent your objects if you know that your objects should have a consistent and restricted set of properties/methods.
It is not possible to add these restrictions without creating a class. It is possible to create a 'helper' class which is not available outside the class in which you define it. To do that, you must define your helper class in the same .as file, but outside the package. I don't usually recommend this, as it's more difficult than standard classes for not much gain.
-
You'd need to write a custom class with those properties declared as public variables (or getter/setter pairs). Aside from giving you typing information in the compiler, this will also let your code run a little faster (since it doesn't need to infer the types at runtime).
Please use [php] or [code] tags, and mark your threads resolved 8)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|