Notes:
- Change return type for
GetInlineKeyboard
to InlineKeyboardMarkup
instead of InlineKeyboardButton[][]
- Use explicit types instead of implicit (
var
)
- Use
List<InlineKeyboardButton>
instead of Array (InlineKeyboardButton[]
)
- Don't create buttons with same callback data
- Replace method parameters to take
IDictionary<string, string>
to get text and callback data
Every IEnumerable<InlineKeyboardButton>
Is single row, This means you should pass list of rows.
You shouldn't use var keyboardInline = new InlineKeyboardButton[1][];
because this means only one row with buttons in this row!
There are two ways below:
Code you wants:
I don't suggest this code
private static InlineKeyboardButton[][] GetInlineKeyboard(string[] stringArray)
{
var keyboardInline = new InlineKeyboardButton[stringArray.Length][];
for (var i = 0; i < stringArray.Length; i++)
{
keyboardInline[i] = new InlineKeyboardButton[]
{
new InlineKeyboardButton
{
Text = stringArray[i],
CallbackData = "Some Callback Data",
};
}
}
return keyboardInline;
}
Best way:
I suggest this code:
private static InlineKeyboardMarkup GetInlineKeyboard(IDictionary<string, string> buttonsData)
{
// Rows Count
int count = buttonsData.Length;
// List of rows
// Every 'List<InlineKeyboardButton>' is row
List<List<InlineKeyboardButton>> buttons = new List<List<InlineKeyboardButton>>(count);
for (int i = 0; i < count; i++)
{
// Add new row with one button capacity
buttons.Add(new List<InlineKeyboardButton>(1)
{
// Add button to this row
new InlineKeyboardButton
{
Text = buttonsData.Values.ElementAt(i),
CallbackData = buttonsData.Keys.ElementAt(i),
}
});
}
return new InlineKeyboardMarkup(buttons);
}