game symbol
game symbol

Reputation: 135

How to set same function property for multiple variable?

I was searching for a shortcut to set same property function to multiple variables.

I am trying to achieve something like {var1, var2, var3}.setSomething(true) and (var1, var2, var3).setSomething(true) but it is not working. Is there anyway to get this done?

instead of

var1.setSomething(true)
var2.setSomething(true)
var3.setSomething(true)

How can I write all these in a same line or setting it only once?

Upvotes: 0

Views: 238

Answers (1)

Benjamin Liu
Benjamin Liu

Reputation: 478

old school way, write a helper function:

public static void setSomeThing(boolean value, Target ... targets){
    if(targets == null)
        return;

    for(Target target : targets){
        target.setSomeThing(value);
    }
}

for using:

setSomeThing(true,var1,var2,var3);

Upvotes: 4

Related Questions