# How to refer to a variable outside a roster?

How can I refer to a certain roster variable outside the roster?

Assumed that I want to refer to the first HH member’s income.
Is this right?: hhroster.income && @rowcode == 1

Thanks you guys!

No, this is not right. `@rowcode` is the rowcode of the current person in the roster, so it is not defined outside the roster. You will have a compilation error if this roster is first nesting, and incorrect/undesired behavior if it is nested into some other roster.

`hhroster[1].income` - will give you the income of the person with `@rowcode` equal to 1.

Great! We can refer to a certain roster.variable now.

But the new problem is that - if we don’t know how many roster tables (like, how many HH members) we will get, it’s that possible to calculate the household income (defined as the aggregated income of all the household members’ income) ?

So if we assume that the maxium number of HH roster is 5, and now we want to sum up individual income as the household income, can we create a calculate hhIncome variable and write the expression: “hhroster[1].income + hhroster[2].income + hhroster[3].income + hhroster[4].income + hhroster[5].income” ?

I have tried it out: no syntax errors after compiling. But it only worked when I input 5 (the maxium HH member number) members. If I only input 3 members, the varialble keeped at “[…]”, and there wasn’t the aggregated number I want.

Thanks Sergiy.

Create a variable outside the roster (double):
`hhroster.Sum(x=>x.income)`

This Overview from Survey Solutions Syntax Guide gives you ideas how to work with rosters.

Yes, I read that article before, but had no idea about how to employ the “sum” function. So, looks like I need to learn much more about LINQ and Lambda expression in C#.

Thanks very much for Peter!

There is a bunch of examples in this public questionnaire:
“Public example User questions and common patterns”, for example the Livestock section.
Best, Sergiy

Cool. Thank you Sergiy!