0

Type of the array

ReplyKeyboardMarkup replyKeyboardMarkup = new(new[]
                {
                 new KeyboardButton[]{ "January", "February", "March", "April"},
                 new KeyboardButton[]{ "May", "June", "July", "August"},
                 new KeyboardButton[]{ "September", "October", "November", "December"},
                })

I have the following code, where ReplyKeyboardMarkup is a custom class from telegram.Bot api from nuget packages. How can I access a specified element, like first string in first KeyboardButton array (January)?

9
  • What have you tried and what didn't work as expected? Commented Sep 26, 2022 at 11:06
  • I expacted it to be smth like: universalLayout.Keyboard[i][j] = "smth new"; Or universalLayout[i][j] = "smth new"; Commented Sep 26, 2022 at 11:07
  • @Palamar66: And what happens when you try that? What even is universalLayout? Commented Sep 26, 2022 at 11:09
  • Once you feed your collection to the ReplyKeyboardMarkup constructor, you are constrained to however that exposes the data. As this is not a class that's part of the standard library you'd have to at least specify what library you're using (a link to the NuGet package would be nice). Commented Sep 26, 2022 at 11:09
  • core.telegram.org/constructor/replyKeyboardMarkup just guessing Commented Sep 26, 2022 at 11:09

1 Answer 1

1

You are looking for the Keyboard property:

string january = replyKeyboardMarkup.Keyboard
        .FirstOrDefault()?.FirstOrDefault()?.Text;

The Text property comes from the KeyboardButton and is what you have specified.

You have commented that you want to access it as a real array, because you want to modify each button easily. Then you can use this approach:

KeyboardButton[][] kbButtonArray = replyKeyboardMarkup.Keyboard as KeyboardButton[][]
            ?? replyKeyboardMarkup.Keyboard.Select(x => x.ToArray()).ToArray();    

Actually the try-cast already works in the current implementation, so no need for the LINQ query. But since it it's an IEnumerable<IEnumerable<KeyboardButton>> that cast might fail in future.

Sign up to request clarification or add additional context in comments.

5 Comments

Thx for answer, I should have specified, that I want to access the value in order to change it inside the Markup.
@Palamar66: What value you want to access? Instead of ...?.Text you can also select the button itself. Then you can modify it. KeyboardButton firstButton = replyKeyboardMarkup.Keyboard.FirstOrDefault()?.FirstOrDefault();
I want to get any value by passing it's index, like the first button row, third button smth like replyKeyboardMarkup[0][2]
Yeah, that wasn't that obvious. Thanks for answer, I'll accept it in a minute.
then just call replyKeyboardMarkup[0][2] to get 2nd element of {"January", "February", "March"}, in you case this will be "March"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.