Correct syntax to create macro with c# statements

I created a macro to count the number of answers with a specific option. I created a variable to receive the result of type long integer with expression $MHCount
The macro was created as

$MHCount

var mhs = new[]{MH1==1, MH2==1, MH3==1};
return mhs.Sum(i => i? 1:0)

The errors reported were:

  • ; expected
  • the name ‘var’ does not exist in the current context
  • the name ‘mhs’ does not exist in the current context
  • the name ‘mhs’ does not exist in the current context

It looks to me that there is problem with macro definition syntax. I tried also with “;” at the end of return statement but did not work either.
C# syntax is correct as I tried this code with normal C# compiler.

Thanks for help

Macros are not functions they are in place text injections.

The main idea is to use tempalted piece of code that will be substituted in the presice location of the refenece to the macro by it’s name.

To manually check the logocal correctness just copy the entire content of the macro to the place of the reference (usage of $macro_name or $MHCount in your case) and see that you’ll receive the same result.

thanks vitalii,
I see this macro in Survey Solutions site.

I copied the macro above from survey solution server and paste into my test questionnaire “in survey solution demo server” and got the same errors above.

Was the use of similar macro dropped from Survey Solutions ?

as @alsaleem has mentioned in his post, his code is supposed to be counting specific values in a number of questions. There is a more elegant way of doing this with the built-in function CountValue(), which was introduced in Survey Solutions specifically for that purpose.

Thanks @sergiy for the link. That’s what I was looking for in this scenario.

For future macro use. I may need to use such macro that has its var and other program logic. The code is correct in C# syntax while I got the above errors. I was using it in a variable expression.

Is it that such macros should not be used “as is” in variable expressions?

There is already a similar sample macro in Survey Solution demo server but was used inside “new Func” block. Is this the only way?

Thanks.