✅ cant switch buttons.
I got a task is to randomize a 4x4 board with 15 visible buttons and one hidden. When a button next to the empty space is clicked, it swaps places with the empty spot—updating its text, background, and visibility. The goal is to sort the buttons in ascending order, with the hidden button in the bottom-right corner.
but my problam is that when i click oln a button whos next to an invisible one this exception :
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
idk whats in the code is out of bounds here. can sombody help me ?
(when the games over theres supposed to be a message box that will ask if you want to continue or not so that why its there at the end)
9 Replies

you access the array[i-4] before you check if i-4 >= 0
thank you
now it says that :

what if i==Button.Length-1
btw do check before access
How to safely cast by using pattern matching and the is and as oper...
Learn to use pattern matching techniques to safely cast variables to a different type. You can use pattern matching as well as the is and as operators to safely convert types.
never mind i think i got it
thank you!
btw i will replace the hard coded stuff to
if you change the 4 to eg 5 then the i%5==3 will just broken if you forget to change it
oh cool ill try that rn