Resolved Changing a sinngle number in a variabel name

Horizon

New member
Joined
Oct 7, 2020
Messages
2
Programming Experience
Beginner
C#:
if(player == "X")
{
    btn1.Content = "X";
    player = "O";
}
else
{
    btn1.Content = "O";
    player = "X";
}

Is there a possibility to just change the 1 from btn1 into a 2? Because i need 9 buttons all doing the same. I would like to creat a method using this code but changing the number infront of the .Content
im open for solutions :D
 
You can use a common event handler and cast the sender parameter to Button, the sender is the button that raised the event.
C#:
((Button)sender).Content = player;
I will also suggest you simplify code with ?: operator - C# reference
 
Why do you want additional method? You only need one method.
 
Back
Top Bottom