Get access to an object's property using bracket notation in dart - properties

I try to do the following:
var properties = ["height" , "width"];
for (var prop in properties){
div.style[prop] = otherdiv.style[prop];
}
But dart doesn't seem to accept this bracket notation, is there any other way to access a property using a string in dart ?

You can use the getPropertyValue and setProperty methods like
div.style.setProperty(prop, otherdiv.style.getPropertyValue(prop));

Related

How do I get an Array from a different class in Kotlin?

How do I access my Arraylist from another class? I am trying to add two variables to a single position.
var nc = findViewById<TextView>(R.id.noteContent) as String
var nt = findViewById<TextView>(R.id.noteTitle) as String
if (!nc.isEmpty() || !nt.isEmpty()) {
get(noteArray(.add(nc,nt)
I am new to Kotlin and I don't know Java. I think I need to use a getter but I am not sure how do it.
My arraylist, "noteArray", always come up as unresolved reference; it's in my main class with the code.
lateinit var noteArray: MutableList<Note>
I am not sure if I need to provide a setter or something within the mainActivity but I think I can do it all from activity2.

How can I initialize a companion object property with a value taken from application.properties?

Is it possible to initialize a property of a companion object with some value read from application.properties?
I tried
// #Value("\${my.property.value}") ...doesn't work
val myProp: Duration by lazy { Duration.ofMinutes(#Value("\${my.property.value}")) }
but IntelliJ complains: Expecting an element.
Okay Duration.ofMinutes() requires a Long not a String but how can I achieve this using a configurable value in application.properties?
I solved it by using the #Value annotation in a service class now.
My intention was to have this value read in into a single place near the entity.

In Kotlin what does this get() do

Im new to Kotlin and wonder what does the get() = login_email.txt.toString() do?
Does it set email String?
get() and set(value) after a field means the declaration of a custom getter and/or setter. Here's a basic example using default values:
class Demo{
var something: String
get() = field
set(value) {
field = value;
}
constructor(something: String){
this.something = something;
}
}
These two are, however, redundant. You don't actually need them unless you're doing something custom with it. They're automatically added for vars, though that only applies to getters for vals (because they can't be changed, they don't have setters).
The line you were asking about is a custom getter.
get() // declares a custom getter
= // if you don't know how this works, see my explanation below
login_email.text.toString() // you should be familiar with this part already; gets the string value of the field
If you're not familiar with the syntax, this is the equivalent without =:
get(){
return login_email.text.toString()
}
if you have a single return, you can replace the brackets and return keyword with =. If it helps you remember, just remember the alternative to using = (a body + the return keyword)
TL;DR: it declares a custom setter that returns the value of a TextView/EditText (not sure which it is, you didn't include that in the question)
In your case, you're using a custom getter or setter to handle property data. The fields themselves don't actually contain any data, but you have getters for a different object.
Take this as an example:
class Demo(private val someObject: MyCustomObjectWithSomeData){
val text: String
get() = someObject.text
... same for other stuff. Could also have setters, if the properties are mutable
}
Here the object is private, but it could be public or internal for that matter.
Kotlin supports quite a lot with custom getters. For an instance, you can declare a field to display specific fields of a private variable. For an instance, in your case, you have the email. It doesn't need to be a variable, since you have a custom getter, and the field isn't initialized. If you change var email to a val, you can make it non-null:
val email: String
get() = login_email.text.toString()
That also helps with null-safety.
And for the error field, it's slightly more complicated. It can't be a val because you declare a custom setter, but if you add a getter, you can make it non-null:
var error: String
get() = login_error.text.toString()
set(value){
login_error.text = value;
}
Short Answer
get() is used to define a custom getter method. Anytime you access the property you are using the custom getter method
Long Answer
So before we can talk about get(), it is important that we get a proper understanding of what properties actually are.
Properties
We all know that in object oriented programming the main idea of a class is to encapsulate data and code that works on data in a single class. In a language like Java (don't worry we will get back to Kotlin soon), the data of a class is stored in private fields, we then use accessor method (getters and setters) to access the data. In Java the combination of accessor methods and a field is called a property
Now in Kotlin does things a little differently, it entirely replaces the traditional idea of defining accessor methods and fields. By using the val or var keyword Kotlin will automatically generate the corresponding field and appropriate accessor methods for us.
get()
There will come a time when either your code or someone else's code needs a more robust solution to the automatic accessor methods created by Kotlin. This is where get() and set() come into play. By using get() you are defining your own custom accessor method(getter for get()) to be used when you are accessing this property.
I would also like to point out that since val is immutable it does not allow you to define a set() method, only a get()

How to find the user's documents directory with Kotlin?

val fr = javax.swing.JFileChooser()
val fw = fr.fileSystemView
val documentsPath = fw.getDefaultDirectory().toString()
println(documentsPath)
I tried this code but IDEA said "Use property access syntax"..
Is there a better way?
"Use property access syntax" is an inspection that "reports calls to java get and set methods that can be replaced with use of Kotlin synthetic properties."
i.e. You can replace fw.getDefaultDirectory() with fw.defaultDirectory.

What is the syntax to declare a unique_Ptr variable in a header, then assign it later in the constructor?

I have coded the following, and am very new to c++, and it feels clumsy. I am trying to give 'spriteBatch' (a unique_Ptr) class scope. Here's the header file:
ref class CubeRenderer : public Direct3DBase
{
public:
CubeRenderer();
~CubeRenderer();
private:
std::unique_ptr<SpriteBatch> spriteBatch;
};
Then in the cpp file Constructor, this:
std::unique_ptr<SpriteBatch> sb(new SpriteBatch(m_d3dContext.Get()));
spriteBatch = std::move(sb);
It just seems clumsy the way I had to create 'sb' and move it to 'spriteBatch'. attempting to assign directly to 'spriteBatch' failed (maybe I simply don't know the proper syntax). Is there a way to avoid needing to use 'sb' & std::move?
Thank you.
The following should work fine:
spriteBatch = std::unique_ptr<SpriteBatch>(new SpriteBatch(m_d3dContext.Get()));
Alternatively, you can avoid repeating the type name with some make_unique function.
spriteBatch = make_unique<SpriteBatch>(m_d3dContext.Get());
There's also the reset member:
spriteBatch.reset(new SpriteBatch(m_d3dContext.Get()));
But, since you mention a constructor, why not just use the member initialization list?
CubeRenderer::CubeRenderer()
: spriteBatch(new SpriteBatch(m_d3dContext.Get())) {}