Update_All with a Method

How do I update a variable in a method each time it is called? (JAVA)

The issue here is that you're creating a brand new Score each time the addScore() method is called. So that is the score that you're updating and not the original Score you created.

There are a lot of different ways to handle the persistence in this case, but perhaps the easiest would be to create an addScore() method within the Score class itself:

class Score {

private int score = 0;

public void addScore() {

score++;

}

public int getScore() {

return score;
}
}

Then, in your main application, create one Score object and call its addScore() method:

public class Main {

public static void main(String[] args) {

Score score = new Score();

for (int i = 0; i < 3; i++) {
score.addScore();
System.out.println(score.getScore());
}
}

}

As I said, though, there are several other options, depending on the overall needs of a real-world application.

Another possibility is to create a single Score as I do in the main() method above and then but pass its reference to the addScore() method as a parameter. In order to do that, you'd need to update the method:

private void addScore(Score score) {
score.addScore();
}

Then call addScore(score); from the main application instead.


You can learn more about the issue you're facing by reading up on the scope of variables in Java.

Use IN clause in updateAll() method in CakePHP 3.x

usinga updateAll or a query() objects to do a bulk update is the same thing as you can read in the manual at the end of this paragraph

so you can do

$this->Leaveregisters->query()
->update()
->set(['leaveregister_status' => $this->request->data('status')])
->where(['leaveregister_id IN' => [1,2,3])
->execute();

or

$this->Leaveregisters->updateAll(
['leaveregister_status' => $this->request->data('status')]
['leaveregister_id IN' => [1,2,3]);

remember then when usin IN clause you have to pass an array. Read this part of the manual on how to create IN clause

Method recursive to update a value down to top

You can simply iterate until the parentId of the current node becomes null.

const data = [{ id: 1, parentId: null, selected: false, children: [{id: 2, parentId: 1, selected: false, children: [{id: 3, parentId: 2, selected: false, children: [{id: 4, parentId: 3, selected: false, children: []}]} ]} ] }, { id: 5, parentId: null, selected: true, children: [] }, { id: 6, parentId: null, selected: true, children: [] } ];
const getById = id => {
const get = arr => {
for(const x of arr){
if(x.id === id) return x;
const res = get(x.children || []);
if(res) return res;
}
}
return get(data);
}
let node = getById(4);
while(node.parentId != null){
node = getById(node.parentId);
node.selected = true;
}
console.log(data);

How can I call a method inside Update only once?

I think one solution could be something like this: store the previous state of gameObjectsInfo and compare to the current gameObjectsInfo. If they are not equivalent then gameObjectsInfo has been changed.

...

public string previousGameObjectsInfo = ""; // to store the previous state
public string gameObjectsInfo = "";

...

private void Update()
{
if(gameObjectsInfo != "" && gameObjectsInfo != previousGameObjectsInfo)
{
Search(); // or anything else
}

previousGameObjectsInfo = gameObjectsInfo;
}

C# Abstract Method to enforce update method

You can avoid hiding a class by type parameter by making a PersistenceEntity class generic itself

public abstract class PersistenceEntity<T> where T : PersistenceEntity<T>
{
public abstract T Update(T existing);
}

It means self-referencing generic constraint, because every class inheriting PersistenceEntity should update an existing instance of itself type.

The implementation for MyClass would be the following:

public class MyClass : PersistenceEntity<MyClass>
{
public override MyClass Update(MyClass existing)
{
existing.SomeProperty = SomeProperty;
existing.SomeProperty2 = SomeProperty2;
return existing;
}
}

Another option is to create an invariant interface, which incapsulates the Update method (if you aren't allowed to make PersistenceEntity class generic)

public interface IUpdate<T> where T : PersistenceEntity
{
T Update(T existing);
}

And then implement it

public class MyClass : PersistenceEntity, IUpdate<MyClass>
{
public MyClass Update(MyClass existing)
{
existing.SomeProperty = SomeProperty;
existing.SomeProperty2 = SomeProperty2;
return existing;
}
}

Using update() method for two duplicate sets

This occurs because you're assigning the return value of .update() to thirdset. The updates occur in-place (i.e. they mutate firstset), and the function returns None.

The code snippet below shows what you were probably intending. Note that firstset and thirdset point to the same location in memory, so updates using firstset will be reflected when attempted to read the contents of thirdset, and vice versa.

firstset = {1, 2, 3}
secondset = {1, 2, 3}
firstset.update(secondset)
thirdset = firstset
print(thirdset)


Related Topics



Leave a reply



Submit