flex 3 iterate through object values - flex3

i have an object which represents a database table. I want to iterate through this object and print printing each value. What can i use to do this?
i want to do this inside my mxml not actionscript
for each object attribute i want to create an imput field

Look up the documentation on Flex 3 looping. If you do, you'll find this:
for..in
The for..in loop iterates through the properties of an object, or the elements of an array. For example, you can use a for..in loop to iterate through the properties of a generic object (object properties are not kept in any particular order, so properties may appear in a seemingly random order):
var myObj:Object = {x:20, y:30};
for (var i:String in myObj)
{
trace(i + ": " + myObj[i]);
}
// output:
// x: 20
// y: 30
Instead of trying to create an input field for each object, I'd suggest you take a look at DataGrid and custom ItemEditors.

I agree that this answer isn't useful. It only works with generic objects, not user declared
objects.
However, here's some code that should/could work using the describeType as suggested above. (And I don't really think it's too complex). Be aware that only public properties/methods, etc. are exposed:
var ct:CustomObject = new CustomObject();
var xml:XML = describeType(ct);
for each(var accessor in xml..accessor) {
var name:String = accessor.#name;
var type.String = accessor.#type;
trace(ct[name]);
}

The problem with "for...in" is that it iterates only on dynamic properties. That is, if your object is defined as a Class (and not dynamically), "for..in" won't give anything.
The ActionScript documentation suggest to use describeType() for fixed properties, but it looks over-complicated for this simple taskā€¦

You can write it like actionscript but include it inside the mxml file with the script tag:
<mx:Script>
<![CDATA[
public function LoopAndPrint() : void
{
//your code here
}
]]>
</mx:Script>

Related

Please Explain Property Delegates

I am trying to learn Kotlin and TornadoFX. One thing I keep seeing is code like this:
val TextProperty = SimpleStringProperty()
var text by Textproperty
I've been reading up on the documentation
https://edvin.gitbooks.io/tornadofx-guide/content/part2/Property%20Delegates.html
so I've "absorbed" that they are useful for when values in a model change, but I need further help really grasping the concept. Something doesn't quite seem to be clicking. Does anyone have any examples, videos, etc. that demonstrate the purpose and usefulness of these property delegates?
The important point here is that JavaFX requires or at least prefers observable properties. Instead of declaring a mutable property, you would declare one of the JavaFX property types, depending on what property type you want (String, Double, Int etc). All you really need is to declare this property:
class Customer {
val ageProperty = SimpleIntegerProperty()
}
You can get by with just this, without using any delegates. However, if you want to mutate this property, you need to change the value property inside the property you defined, so the code looks like this:
customer.ageProperty.value = 42
This is cumbersome, so to make it simple, you'd want to add getters and setters. Doing this manually would look something like this:
val ageProperty = SimpleIntegerProperty()
var age: Int
get() = ageProperty.value
set(value) { ageProperty.value = value }
Now you can set the age of a Customer like this:
customer.age = 42
This is much more convenient, but you'd have to declare all that code for each property, so in TornadoFX we introduced property delegates that takes care of all that with a simple statement, so:
val ageProperty = SimpleIntegerProperty()
var age by ageProperty
The end result is the same, without you having to do any of the heavy lifting.
Just to be clear, this explains the property delegates, not why JavaFX properties are useful. JavaFX properties allow you to bind to a UI element, like a text field for example. Changes to the text field will be written back into the property, and changes to the property would be written back to the UI.
Once you can written something like
class C { var text by Textproperty }
all c.text = blabla will be compiled to TextProperty.setValue(c, ::text, blabla), and c.text will be compiled to TextProperty.getValue(c, ::text). This enables the library (the provider of TextProperty) to take over the operations on text.
Ref: https://kotlinlang.org/docs/reference/delegated-properties.html

How to add class level variable declarations using javaparser?

class A{
int x = 10;
}
This is A.java
I want to get NewA.java
class NewA{
int x = 10;
Sting text = "B";
}
I want to add a variable using javaparser.
You need to do this:
Parse the code
Find the point where you want to add your element
Add the element you want
Dump back the code
The first point is trivial, just use the JavaParser.parse method. You will get a CompilationUnit. In the example you shown you are adding a field in a class declaration, so you need first to get that class declaration. Call getTypes and look into that list for the declaration you want or just call getClassByName.
Once you have your class declaration you can call addMember on it. In your example you are adding a field so you need to instantiate a FieldDeclaration.
Once you are done you take your CompilationUnit and call toString. You will get back the modified source code.
Source: I am a JavaParser committer

lua / luabind - Add and overwrite class methods through lua

I'm using lua 5.2.2 with luabind 0.9.
I'd like to be able to add additional class-methods through lua for any classes that I've bound in c++, but I'm unsure how to do it.
The problem is that luabind uses a function as the __index-metamethod for any bound classes instead of a table, so I don't see a way to access the class-methods at all.
e.g., I'm binding my classes like this:
luabind::module(lua)
[
luabind::class_<testClass>("TestClass")
.def(luabind::constructor<>())
.def("TestFunc",&TestFunc)
];
What I essentially want to do is to add a lua-function to the list of methods for this class, and be able to overwrite existing ones:
local t = tableOfClassMethods
local r = t.TestFunc -- Reference to the c++-function we've bound
t.SomeFunction = function(o) end -- New function for all objects of this class
t.TestFunc = function(o) end -- Should overwrite the c++-function of the same name
Any help would be appreciated.
You could use a luabind::object property and register it with the .property method of luabind
Something like this:
//Class
class FunctionCaller
{
public:
luabind::object Fn;
void SetFn(luabind::object NewFn)
{
Fn = NewFn;
};
luabind::object GetFn()
{
return Fn;
};
};
//Binding Code
luabind::class_<FunctionCaller>("FunctionCaller")
.def(luabind::constructor<>())
.property("Fn", &FunctionCaller::Fn, &FunctionCaller::SetFn, &FunctionCaller::GetFn)
Then you just need to call the luabind::object according to the luabind docs.
It's not exactly what you want to do but it could help you overwrite the function I think. You could bind the real function and have a property, check if the luabind::object is non-null, and call it or the native function.

mimicking MVC DisplayExtensions methods (fun and confusion with lambdas)

I'm trying to develop my own editable data grid for MVC in a fluent interface usable in a View. My question is really about using lambdas, not about data grids, but the context might help for understanding my question. Anyway, I have some code that looks like this:
#model IEnumerable<AdamOneilSoftware.Models.Something>
#{
var firstItem = Model.FirstOrDefault();
}
#(Html.DataGrid(Model)
.CssClass("data")
.TextBox(model => firstItem.Text)
.TextBox(model => firstItem.Position)
.Render())
This much compiles okay. You can see that I want to use lambdas to setting up columns of my data grid. The thing I don't understand is how to do anything with those lambdas in my data grid class. I have surmised that I need to compile/invoke the expression, but I'm stuck on the Invoke part. Here's what I have:
public DataGridBuilder<TModel> TextBox<TValue>(Expression<Func<TModel, TValue>> expression)
{
var del = expression.Compile();
// invoke the delegate?
var result = del.Invoke(); // but this expects a TModel argument
return this;
}
The Invoke call expects a TModel argument (according to the Func declaration -- I understand that). But this doesn't work--and rightly so:
var result = del.Invoke(TModel);
How do I form the TModel argument in the Invoke call? Or am I heading the wrong direction completely? The goal is to do something like what the DisplayFor and EditorFor methods do--render some HTML with some awareness of the target property's metadata (DataFormatString, Display label, and so on).
You need to get the view's Model instance to pass as the parameter.
You can find that in Html.ViewData.Model.

Scala class inheritance

Tagged as homework.
I'm having trouble in the object oriented world while trying to implement a class.
I'm implenting various functions to take action on lists, that I'm using to mock a set.
I'm not too worried about my logic on how to find union, for example, but really just the structure.
For eg:
abstract class parentSet[T] protected () {
def union(other:parentSet[T]):parentSet[T]
}
Now I want a new class extending parentSet:
class childSet[T] private (l: List[T]) extends parentSet[T] {
def this() = this(List())
private val elems = l
val toList = List[T] => new List(l)
def union(other:parentSet[T]):childSet[T] = {
for (i <- this.toList) {
if (other contains i) {}
else {l :: i}
}
return l
}
}
Upon compiling, I receive errors such that type childSet isn't found in def union, nor is type T to keep it parametric. Also, I assume my toList isn't correct as it complains that it isn't a member of the object; to name a few.
Where in my syntax am I wrong?
EDIT
Now I've got that figured out:
def U(other:parentSet[T]):childSet[T] = {
var w = other.toList
for (i <- this.toList) {
if (!(other contains i)) {w = i::w}
}
return new childSet(w)
}
Now, I'm trying to do the same operations with map, and this is what I'm working on/with:
def U(other:parentSet[T]):MapSet[T] = {
var a = Map[T,Unit]
for (i <- this.toList) {
if (!(other contains i)) {a = a + (i->())}
}
return new MapSet(elems + (a->()))
}
I still want to use toList to make it easily traversable, but I'm still getting type errors while messing with maps..
This code has a few problems:
It seems that you are not realizing that List[T] is an immutable type, meaning you cannot change its value once created. So if you have a List[T] and you call the :: method to prepend a value, the function returns a new list and leaves your existing one unchanged. Scala has mutable collections such as ListBuffer which may behave more like you expect. So when you return l, you're actually returning the original list.
Also, you have the order wrong in using ::. It should go i :: l, since :: is a right-binding function (because it ends with a :).
Lastly, in your union method you are doing (other contains i). Maybe it's just the Scala syntax that's confusing you, but this is the same as doing (other.contains(i)) and clearly contains is not a defined method of parentSet. It is a method on the List[T] type, but you're not calling contains on a list.
You tagged this as homework so I'm not going to fix your code, but I think you should
Look at some examples of correct Scala code involving lists, try here for starters
Play around in the Scala REPL and try creating and working with some lists, so you get a feel for how immutable collections work.
To answer your direct question, even though childSet is inheriting parentSet the original method specify parentSet as the return type and not childSet. You can either only use parentSet as the type OR you could specify the return type to be anything that inherits parentSet.