get.Checkbox() issue: not collecting

Hi all,
I’m trying to write a simple macro based on a series of checkboxes with the following code:

WiseChoices=newArray();

Dialog.create("A General question?");
columns = 1;
labels = newArray("Statement 1", "Statement 2");

CollectChoices();

function CollectChoices(){
	n=labels.length;
	rows = n;
	defaults = newArray(n);
	Dialog.addCheckboxGroup(rows,columns,labels,defaults);
	Dialog.show();
		for (i=0; i<n; i++){
		print(Dialog.getCheckbox());
		WiseChoices=Array.concat(WiseChoices, Dialog.getCheckbox());
		}
 	Array.print(WiseChoices);
	}

At the end of the macro, after running several dialogs, I would like to collect the WiseChoice array and compare it to standard arrays. The problem is that Dialog.getCheckbox() issues an error message:

Dialog.getCheckbox(<)>

and the array is not filled with the answers.

Can you help?

Sincerely,

Matthieu

You’re calling Dialog.getCheckbox() twice within each iteration of the for loop (once in the print statement, and when concatenating arrays).

So at the second iteration, there will be no checkbox left to be read from the dialog, as you already called both of them in the first iteration.

Try instead to put the checkbox state in a variable that can be reused in the loop:

WiseChoices=newArray();

Dialog.create("A General question?");
columns = 1;
labels = newArray("Statement 1", "Statement 2");

CollectChoices();

function CollectChoices(){
	n=labels.length;
	rows = n;
	defaults = newArray(n);
	Dialog.addCheckboxGroup(rows,columns,labels,defaults);
	Dialog.show();
		for (i=0; i<n; i++){
			result = Dialog.getCheckbox();
			print(result);
			WiseChoices=Array.concat(WiseChoices, result);
		}
 	Array.print(WiseChoices);
	}

Thanks! That’s indeed a mistake. But once removed, there is another issue:

WiseChoices=newArray();

Dialog.create("A General question?");
columns = 1;
labels = newArray("Statement 1", "Statement 2");

CollectChoices();

Dialog.create("Another General question?");
columns = 1;
labels = newArray("Statement 3", "Statement 4", "Statement 5");

CollectChoices();

Array.print(WiseChoices);

function CollectChoices(){
	n=labels.length;
	rows = n;
	defaults = newArray(n);
	Dialog.addCheckboxGroup(rows,columns,labels,defaults);
	Dialog.show();
		for (i=0; i<n; i++){
			result = Dialog.getCheckbox();
			WiseChoices=Array.concat(WiseChoices, result);
		}
	}

If you run this: WiseChoices returns empty when you expect the array to contain five numbers. Curiously, if I stubbornly repeat the commands instead of running a function, it works, but it’s not very nice. Any idea why?

Sincerely,

Matthieu

I’ve received the following answer - off forum - from Herbie Gluender:

Matthieu,

you need to consider how Java deals with arrays that are passed to subroutines (methods).

WiseChoices=Array.concat(WiseChoices, result);

WiseChoices is not the same array anymore after Array.concat().

It is a new array having the same name but not the same address.

Therefore it is not accessible from the man macro section.

I suppose this means that I cannot run a function that automatically updates and array…

You can make it a global variable by adding a var statement the first time the variable is assigned:

var WiseChoices=newArray();

See the macro language documentation.

1 Like