Hi!
I have the following classes:
ActionScript Code:
[LEFT]package [COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]import[/COLOR] flash.[COLOR=#000080]display[/COLOR].[COLOR=#000080]Sprite[/COLOR];
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000]**class**[/COLOR] Main [COLOR=#0000FF]extends[/COLOR] Sprite
[COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000]**function**[/COLOR] Main[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]
[COLOR=#000000]{[/COLOR]
[COLOR=#000000]**var**[/COLOR] myLanguage:[COLOR=#0000FF]Language[/COLOR] = [COLOR=#000000]**new**[/COLOR] [COLOR=#0000FF]Language[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#0000FF]trace[/COLOR][COLOR=#000000]([/COLOR]myLanguage.[COLOR=#000080]user_feedback_password_error[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#000000]**var**[/COLOR] correctMessage:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]"user_feedback_password_error"[/COLOR];
[COLOR=#0000FF]trace[/COLOR][COLOR=#000000]([/COLOR]myLanguage[COLOR=#000000][[/COLOR]correctMessage[COLOR=#000000]][/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#000000]**var**[/COLOR] faultyMessage:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]"test"[/COLOR];
[COLOR=#0000FF]trace[/COLOR][COLOR=#000000]([/COLOR]myLanguage[COLOR=#000000][[/COLOR]faultyMessage[COLOR=#000000]][/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[/LEFT]
ActionScript Code:
[LEFT]package
[COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000]class[/COLOR] [COLOR=#0000FF]Language[/COLOR]
[COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000]var[/COLOR] user_feedback_password_error:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]“the entered password didn’t match the user name.”[/COLOR];
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000]var[/COLOR] user_feedback_username_error:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]“the entered username doesn’t exist.”[/COLOR];
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000]var[/COLOR] user_feedback_password_succes:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]“you have been successfully logged in.”[/COLOR];
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000]**function**[/COLOR] [COLOR=#0000FF]Language[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]
[COLOR=#000000]{[/COLOR]
[COLOR=#808080]*// this doesn't do anything...*[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[/LEFT]
The way I want to use the Language class is to keep all the language sentences, e.g. for use with a login form, in one class. This way I only have to edit one class if I want to change the language. I store these sentences in public properties, so that when I create an instance of the class I can use the properties with the dot syntax.
The Main class shows three ways of how to use it, in the trace commands:
[LIST=1]
[*]
ActionScript Code:
[LEFT][COLOR=#0000FF]trace[/COLOR][COLOR=#000000]([/COLOR]myLanguage.[COLOR=#000080]user_feedback_password_error[/COLOR][COLOR=#000000])[/COLOR];
[/LEFT]
[*]
ActionScript Code:
[LEFT][COLOR=#000000]**var**[/COLOR] correctMessage:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]"user_feedback_password_error"[/COLOR];
[COLOR=#0000FF]trace[/COLOR]COLOR=#000000[/COLOR];
[/LEFT]
[*]
ActionScript Code:
[LEFT][COLOR=#000000]**var**[/COLOR] faultyMessage:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]"test"[/COLOR];
[COLOR=#0000FF]trace[/COLOR]COLOR=#000000[/COLOR];
[/LEFT]
[/LIST]
The first way works, because the “user_feedback_password_error” property exists in the class.
The second way works as well, because the message string is interpreted by the class as a property name.
The third way doesn’t work, obviously, because there is no property of the name “test” defined in the Language class.
So the way I want to use is the dynamic way using the brackets, because that would make things a lot easier. Now, the third way throws the following error: “Error #1069: Property test not found on Language and there is no default value.” So I was thinking: is there a way to set this default value? That way I could, for instance, return an empty string so a function knows the property doesn’t exist. I’m aware of the strange way of thinking…
I also quickly looked into the try…catch() statement:
ActionScript Code:
[LEFT][COLOR=#0000FF]try[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]trace[/COLOR]COLOR=#000000[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#0000FF]catch[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]trace[/COLOR]COLOR=#000000[/COLOR];
[COLOR=#000000]}[/COLOR]
[/LEFT]
But I couldn’t think of a way to ‘fix’ my problem.
I’m hoping someone here can help me!
Thanks a lot in advance!
- AmiKaze