okay. so i have a function and i've passed a parameter through it called objectName.
no i have no idea how to do this or explain it. so here goes.
public function moveObject(ObjectName):void{
//objectName now holds "myName" which is an object also
//i would now like my variable called myNamePosition to equal 10
//so it would need to grab the value of objectName which is myName:Object.
//turn it into a string of some kind - myName:string
//add "Position" to the end of it so its myNamePosition
// make it equal to 10
trace(myNamePosition);
}
The functions parameters passed through would change so i cant actually use "myName". but rather "objectName".
Thanks
Example:
package
{
import flash.display.MovieClip;
public class astest extends MovieClip
{
public function astest()
{
init();
}
private var myNamePosition:int;
private function init():void
{
moveObject({myName:{}})
}
public function moveObject(objectName:Object):void
{
var propName:String;
for(propName in objectName)
break;
trace(propName);
propName+="Position";
trace(propName);
this[propName] = 10;
var propValue:* = this[propName];
trace(propValue);
}
}
}
output:
myName
myNamePosition
10
Is it what you need?
Related
I need to filter a collection with dynamic properties selection.
Example:
public class NotificationListModel : Observable
{
private string _QMTXT;
public string QMTXT
{
get { return _QMTXT; }
set { _QMTXT = value; RaisePropertyChanged("QMTXT"); }
}
private string _PRIOK;
public string PRIOK
{
get { return _PRIOK; }
set { _PRIOK = value; RaisePropertyChanged("PRIOK"); }
}
private string _ARBPL;
public string ARBPL
{
get { return _ARBPL; }
set { _ARBPL = value; RaisePropertyChanged("ARBPL"); }
}
private string _id;
public string id
{
get { return _id; }
set { _id = value; RaisePropertyChanged("id"); }
}
}
And I have a collection NotificationCollection, is having couple of records, so I need to filter this collection with different properties and those are not fixed like below,
Example 1:
var Result = NotificationCollection.Where(w =>(w.QMTXT=="1" || w.QMTXT=="2") && w.PRIOK == "1").ToList();
Example2:
var Result = NotificationCollection.Where(w =>w.id=="1" && w.PRIOK == "1").ToList();
Here, while filtering the list properties will be dynamic it may filter with QMTXT or PRIOK or combination of QMTXT and PRIOK and some other property.
How can I achieve it.
I did lot of research I came to know we can do this by using reflection but I don't have that much scope on Reflection.
Your help is very appreciable.
Thanks in advance.
System.Linq.Dynamic may be for you: https://dynamiclinq.codeplex.com/documentation
I recently had a similar problem with the ORDER BY statement via Linq. With Linq.Dynamic was possible to change the ordering of fields and the criteria for each.
I have some problem, so I have a few global fields in class, for each I want to do the same code but I don't want to repeat code - just use one method for that. And there I want to send these global fields in argument of this method and as second argument I want to send value for this field.
I tried with object, generic type but I don't know how to do that. Here is example:
private void setName(String _name) {
if(isNull(_name)) {
this.name = "";
} else {
this.name = _name.toString();
}
}
And a few other methods use the same code but with other fields and argument and I want to do something like that:
private void setField(some_field, _value) {
if(isNull(_value)) {
this.some_field = "";
} else {
this.some_field = _value;
}
}
Could someone help?
For example I have 2 global fields:
String name, int age.
For them I need to use the same code (if) and I want do it in one method. In this case I have to use global field as argument and as second argument use correct value for this field so instead:
this.name = argument;
this.age=argument;
use: globa_field_argument = argument;
Example:
setField( this.name, "Test" );
setField( this.age, 5 );
First off I would probably declare your global fields as 'public static'.
for example:
public static int myNumber = 10;
public static String myString = "foo";
Secondly you should have to pass your fields as an argument since you can access them inside the method itself.
When you redefine the fields anywhere in your code you will do this:
this.myNumber = newNumber;
this.myString = newString;
I am kinda new to AS3, and I want to figure out how classes and strings work. I have two files:
Main.as:
package
{
import flash.display.*;
public class Main extends Sprite
{
public function Main():void
{
var f1:Flower = new Flower("rose");
var f2:Flower = new Flower("cactus");
var f3:Flower = new Flower("fff");
}
}
}
Flower.as:
package
{
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Flower
{
protected var name:String;
public function Flower(name:String):void
{
setName(name);
var updateTimer:Timer;
updateTimer = new Timer(500.6, 2);
updateTimer.addEventListener(TimerEvent.TIMER, TimerFun);
updateTimer.start();
}
public function TimerFun(e:TimerEvent):void
{
trace ("test " + getName());
setName("xxx");
}
public function setName(name:String):void
{
this.name = name;
}
public function getName():String
{
return name;
}
}
}
So basically from what I understand, every time I create an instance to the class "Flower.as" like this:
var f1:Flower = New Flower("rose"));
it saves each instance to a new line in the protected var name which is located at flower.as. then I can get the names with getnames. I placed a timer to check the values, and inside the timer I decided to try to assign a new name with the function setName("xxx"), and I set the timer to activate itself twice in order to see how the results change; However, all it did was replace all of the instances with the value "xxx", it didn't add anything(I expected a new string named "xxx" along with the others). I need an explanation about what
var f1 Flower:Flower = New Flower("rose");
does exactly and what setName("xxx") does. Thank you for your time.
Okey let me try to explain it for you :)
When u creat this : var f1:Flower; = new Flower("rose"); u creat a flower and give it name as "rose". But however when u creat a Flower type variable it trigger a TimerEvent. When TimerEvent start working first line will give to you
"test rose" on your output panel. But the second line is using setName("xxx") function for changing Flower's name.
So when u look your output panel u should see something like this :
test rose
test xxx
I hope this will enough to understand. If u have a question just ask :)
I was looking into Haxe abstracts and was very interested in building an abstract that would wrap a class and unify it to, in my case, an Int.
#:forward()
abstract Abs(Op)
{
public inline function new(value:Int = 0, name:String = "unnamed" )
{
this = new Op();
this.value = value;
this.name = name;
}
#:to
private inline function toInt():Int
{
return this.value;
}
}
class Op
{
public var value:Int = 0;
public var name:String = "no name";
public function new()
{
}
}
The problem I ran in to is when defining a #:from method - it has to be static and can take only one parameter - a new value. So whenever I set the abstract's instance value from the #:from method I will have to create a new instance of the abstract, thus resetting all the variables.
Basically what I'm talking about is this:
var a = new Abs(5, "my abs"); // value is 5; name is "my abs"
a = 100; // value is 100; name is reset to "unnamed" but I want it to be preserved
As much as I could find out we cannot overload the = operator in abstracts other than through implicit casting with a #:from method and I haven't found a way to really achieve this with macros.
If you have any ideas on how this can be done, please provide a minimalist example.
It depends what you want to do, but if you use this:
var a = new Abs(5, "my abs");
var myInt:Int = a;
It will use the abstract Abs.toInt function.
#:to
private inline function toInt():Int
{
return this.value;
}
The other way around also works:
var million = 1000000;
var myAbs:Abs = million;
It will use the static Abs.fromInt function.
#:from
static inline function fromInt(value:Int)
{
return new Abs(value, "what");
}
This is because it uses the implicit cast. http://haxe.org/manual/types-abstract-implicit-casts.html
Try it yourself: http://try.haxe.org/#Ae1a8
Is that what you are looking for?
I need to be able to call a method and pass in an object of an unknown type
but then have the correct overload called. I also need a default implementation that accepts
object as its parameter type. What I'm seeing is that the default overload is the only one that ever gets used.
Here's the gist of what I'm trying to do:
class Formatter
{
private object Value;
public Formatter(object val){
Value = val;
}
public override string ToString()
{
return Format(Value);
}
private string Format(object value)
{
return value.ToString();
}
private string Format(DateTime value)
{
return value.ToString("yyyyMMdd");
}
}
Ok, so far so good. Now I want to be able to do this:
public static class FancyStringBuilder()
{
public static string BuildTheString()
{
var stringFormatter = new Formatter("hello world");
var dateFormatter = new Formatter(DateTime.Now);
return String.Format("{0} {1}", stringFormatter, dateFormatter);
}
}
The result of FancyStringBuilder.BuildTheString() is "hello world 2012-12-21 00:00:00.000", when I expected "hello world 20121221"
The problem is that the overload that accepts a DateTime is not being called, instead defaulting to the overload which accepts an object. How can I call the proper method without resorting to a messy switch statement?
In Formatter.ToString(), the override Formatter.Format(object) is always called. This is because the overload resolution happens at compile-time, not run-time. At compile-time, the only thing known about Value is that it's an object.
If you really want to distinguish incoming types, you'll need to do so in Formatter's constructor. In this case, rather than hanging on to the object, you could just call ToString() immediately and only store the formatted result:
class Formatter
{
string formattedValue;
public Formatter(object value)
{
formattedValue = value.ToString();
}
public Formatter(DateTime value)
{
formattedValue = value.ToString("yyyyMMdd");
}
public string ToString()
{
return formattedValue;
}
}
Note that this does assume that your object isn't changing between the time you create the Formatter object and the time Formatter.ToString() is called, or at the very least that it's okay to take a snapshot of the string representation at the time the Formatter is created.
This also assumes that you know the incoming types at compile-time. If you want a truly run-time-only solution, you'll have to use the "is" operator or a typeof() comparison.
If your goal is just to provide custom ToString() formatting based on the incoming type, I'd probably do it using a list that maps from types to format strings:
static class Formatter
{
private static List<Tuple<Type, string>> Formats;
static Formatter()
{
Formats = new List<Tuple<Type, string>>();
// Add formats from most-specific to least-specific type.
// The format string from the first type found that matches
// the incoming object (see Format()) will be used.
AddMapping(typeof(DateTime), "yyyyMMdd");
// AddMapping(typeof(...), "...");
}
private static void AddMapping(Type type, string format)
{
Formats.Add(new Tuple<Type, string>(type, format));
}
public static string Format(object value)
{
foreach (var t in Formats)
{
// If we find a type that 'value' can be assigned to
// (either the same type, a base type, or an interface),
// consider it a match, and use the format string.
if (t.Item1.IsAssignableFrom(value.GetType()))
{
return string.Format(t.Item2, value);
}
}
// If we didn't find anything, use the default ToString()...
return value.ToString();
}
}
With that, calling code then looks like:
Console.WriteLine(
"{0} {1}",
Formatter.Format(DateTime.Now),
Formatter.Format("banana"));
I think this is because the class constructor takes an object as parameter, and then assign that object to variable Value which is also an object. There for calling Format(object) since Value is of type object
Try this
public override string ToString()
{
if(Value is DateTime)
return Format(Convert.ToDateTime(Value)); //this should call the right method
return Format(Value); //works for other non-custom-format types e.g. String
}