I define a variable which is dynamicaly changes based on the user interactions, for example ID of an object sets to variable when user touches on it. After the ID sets I call a function in a custom component which is related to that object. Like this;
activeObject.videoPlay(event) ---> if the activeObject is video1 ---> video1.videoPlay(event) function will be called.
I tried several variable types when defining the variable activeObject, such as String , Array but didnt work out. By the way the data set to variable is String. When I use String type it gives this error;
Error #1061: Call to a possibly undefined method videoPlay through a reference with static type String.
Is there any way to use a string as a dynamic variable?
Is there any way to use a string as a dynamic variable?
Bracket notation -- obj["dynamicPropertyName"] as Type or in your case (activeObject['videoPlay'] as Function).apply(abc, [event]); You'll obviously want null object checks etc.
Related
I already check this post
I have a similar problem
I have a variable object with attributes others objects
so I Have the main object and I want put null to one of their properties
I tried this options but they fail of course, before each line I put the error that give me
set object.other = null
--------------------------------------
Unexpected token "punctuation" of value "." ("end of statement block" expected)
set object["other"] = null
--------------------------------------
Unexpected token "punctuation" of value "[" ("end of statement block" expected)
set (object, 'other', null)
--------------------------------------
Only variables can be assigned to. Unexpected token "punctuation" of value "(" ("name" expected)
set object = object|merge({'other',null})
--------------------------------------
The merge filter only works with arrays or "Traversable", got "object" as first argument
Please Help
The first three of your examples are not valid Twig syntax, like the error messages say.
The fourth example, using Twig's merge filter, would work if your object was an array, a hash (i.e. {% set object = { other: 'something' } %}), or an instance of a Traversable class. Your object is none of these, as can be seen from the error message returned by Twig: 'The merge filter only works with arrays or "Traversable", got "object" as first argument.' Note also that you have a typo (as already mentioned by aferber): you have {'other', null} whereas you should have {'other': null} (or {other: null}).
Thus, what you are trying to do can't be done out-of-the-box in Twig. You need to either modify your class (e.g. create a method which sets the other property to null) or create a Twig extension. The question Adding a property to an object in twig has answers showing how you could approach these solutions.
A third possibility would be to modify your class so that the object is Traversable. Then you could use the merge filter.
Of course one more possibility is to stop trying to do this in Twig and do this e.g. in a controller (as already suggested by Frank B).
In order for this to work, object has to be an object in the first place. The error message to your last try (merge filter) clearly shows this isn't the case (in PHP, every object is Traversable). In addition, the object has to have a public property named other.
You should also pay attention to the difference between {'other',null} and {'other': null}.
After you have fixed all those problem (ie. made sure you assign an object to object first), the merge filter will work, just as it did in the question you linked to.
I have a session variable that i create dynamically, so lets say for instance i have the following session variables
Session["area1"]
Session["area2"]
Session["area3"]
Session["area4"]
And in each of those sessions i have a List in there
then i have this in order to get what the name will be and use it in my code
string areaName = "area" + Session["area"];
Session["area"] is a session variable that increases it self based on how many times a button is clicked
Now if i try to pass areaName to a function that is requiring a List as a parameter it doesn't let me do it, even though the value in that session variable is a list
I am only using "areaName" to be able to get the name of the session
How can i use it in order to pass it to function that is requiring a List type?
If you know it contains a list, then do the cast. For instance
myMethod(Session["area1"] as List<string>);
By calling the following:
string areaName = "area" + Session["area"];
You are saying, take the string "area", and append to that the variable from Session (which is an object by default), which will call it's ToString() method. Your end result here, is a string, not a list. (And not a meaningful one at that).
All you need to do is take the Session value, and cast it to the type that you know it is.
method((List<string>)Session["area"])
or
method(Session["area"] as List<string>)
The reason you can't just pass in Session["area"] without first casting, is because session returns your variable as an object. Sure, it may be a list in memory, but the system treats it the same as any other object (Look up Polymorphism for more info), which is not the List that it expects. Thus, you will get a compile time error, unless you cast it to the correct type.
I've got an object with a list defined inside it which points to a type that can be inherited. From what I understand MVC's default model binder will always instance the base type when reading data back in to this array from a form so by default I will have a list of base types.
So I need to use my own model binder and override CreateModel to instance a specific type (say from a hidden field). However when I do this and use
bindingContext.ValueProvider.GetValue("ModelType")
it always returns null even though through using fiddler I can see that form value Settings[0].ModelType contains my objects type and I need this value in CreateModel to instance the correct type.
Solved it. If your array objects need to be typed based on each item you need to use the following call to get "into" the array item
bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".ModelType")
I'm not sure if this is the standard way to do it. If anyone has any better suggestions feel free to add them
Is it possible to create a new array in windows workflow? More specifically, in the designer.
I've created a variable of System.Int32[], but when I use it I get a NullReferenceException.
I've tried New Int32(5), and various permutations of Dim - nothing I have tried has worked.
I was able to create the array and pass it as an in/out parameter - this works, however the workflow will need to determine the actual size of the array.
To create and instantiate an array, you have to set a default value to your variable with New Int32(FOO SIZE){} or use an Assign activity to instantiate it with the correct size in runtime
You can also use List(Of T) or any other .NET collection structure to achieve dynamic size.
Note that the value must be the right part of a set expression. So, you can google how to do it in VB.NET and you will be fine.
I assume that if you are creating the array in the designer, as you stated, it is either a workflow variable or a workflow argument. The "WF" way to do this would be to use the "Default Value" column under the "Variables" and/or "Arguments" tab.
If it is an argument then the Default Value column only works if the Direction is "In". If your argument is a property, or an Out, or In/Out direction then you would have to use the method mentioned by Davi.
If you are creating it under the "Variables" tag then using the Default value column would be the more built-in approach. The syntax in the default column would be the same syntax mentioned by Davi: New Int32(FOO SIZE) {}
I know that in C# when you pass an object (non primitive) to a method the following is true:
A reference to the object is passed
Changes made to the object in the method are reflected outside of the method.
Also, you can pass a reference to a reference in C# e.g this.changeObject(ref myObject);, in which case:
Changes to the object and also to the ref are reflected outside of the method e.g. myObject = new new List(); would change the passed objects referred location.
My question:
Is this possible to do in Flex/Actionscript - can a ref keyword be used?
No, you cannot. ActionScript doesn't have a ref keyword or a similar (double pointer like) concept. You always pass object references to functions (except for primitives) and modifications are reflected back.