Why index again?

Help please. I can’t understand why it is necessary to put down button indexes here? (highlighted in red in the screenshot)

Hi Maksim,

Gary’s idea is the following: He wants to connect the button with answers. However, the buttons do not have anything to do with answers. For this reason, he uses a common technique to connect two things that do not have anything to do with one another.

We have a list with questions/answers. In C#, each element in an array or collection is associated with an index, which is an integer >= 0. Can we exploit the fact that the elements are associated with numbers?

Yes, of course. We do that by associating each button with a number, too. Then we can compare two integers: the button integer and the answer integer.

In your screenshot, we associate numbers with buttons by passing on an integer to the OnAnswerSelected method.

Did this clear it up for you?


See also:

1 Like

Ahhhhh, okay, thank you. I thought that when I create SO with “string answer = new string[4]” and that highlighted indexes of answer from 0 to 3 for C#… and we can use it now

Yes, exactly. In line 14, you create an array object with 4 string objects. And due to the array, the string objects are mapped to integers. Here: 0 to 3.

The connection between the buttons and this array happens via the GetAnswer method. The buttons pass on a value, and your code uses that value to access an object in the answer array via its index.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms