0

I'm doing a tictactoe project for school and I can't seem to fix my unreachable code problem. I've looked up other Unreachable code issues but I'm not sure how to fix it since I'm not very good at this. I know that its a warning but when i start my program it wont fill in the blanks when i press em.

    private void Movement(object sender, EventArgs e)
    {
        if (InvokeRequired)
        {
            this.Invoke(new Action<object, EventArgs>(Movement), new object[] { sender, e });
            return;
            UpdateData(sender); //This is where it points for unreachable
            var lbl = (Label)sender;
            lbl.Enabled = false;
            lbl.Text = currentPlayer.ToString();
            if (!CheckWin())
                NextTurn();

        }
    }
    private void UpdateData(object sender)
        {
            Label lbl = sender as Label;
            if (lbl == lbl00)
                GameData[0, 0] = currentPlayer;
            else if (lbl == lbl10)
                GameData[1, 0] = currentPlayer;
            else if (lbl == lbl20)
                GameData[2, 0] = currentPlayer;
            else if (lbl == lbl01)
                GameData[0,1] = currentPlayer;
            else if (lbl == lbl11)
                GameData[1, 1] = currentPlayer;
            else if (lbl == lbl21)
                GameData[2, 1] = currentPlayer;
            else if (lbl == lbl02)
                GameData[0, 2] = currentPlayer;
            else if (lbl == lbl12)
                GameData[1, 2] = currentPlayer;
            else if (lbl == lbl22)
                GameData[2, 2] = currentPlayer;
        }
  • 3
    You have a `return` statement above that line, so it will always return and never do the unreachable code. What is the `return` for? – doctorlove Jun 11 '18 at 14:46
  • The `return` after this line `this.Invoke(new Action(Movement), new object[] { sender, e });`will get out of the whole method, so all lines after return will not be executed. – Kaj Jun 11 '18 at 14:48

2 Answers2

5

Because you put a return after this.Invoke(new Action<object, EventArgs>(Movement), new object[] { sender, e }); line so other lines are not reachable

Hesam Faridmehr
  • 1,176
  • 8
  • 20
1

Just to add to the more obvious answers, you haven't implemented the "Invoke on UI Thread" pattern correctly. You either need to replace the return with an extra else branch:

private void Movement(object sender, EventArgs e)
{
    if (InvokeRequired)
    {
        // Oops, we're not on the UI thread. Invoke on the UI thread.
        this.Invoke(new Action<object, EventArgs>(Movement), new object[] { sender, e });
        ... nasty exceptions happen if you try and update UI controls on a non-UI thread
    }
    else
    {
        // Now we're on the UI thread
        UpdateData(sender);
        ... clear to interact with UI controls.
    }
}

Although you COULD also refactor the code to exit the non-UI thread branch early, using return, and then drop the else.

private void Movement(object sender, EventArgs e)
{
    if (InvokeRequired)
    {
        // Oops, we're not on the UI thread. Invoke on the UI thread.
        this.Invoke(new Action<object, EventArgs>(Movement), new object[] { sender, e });
        return; // i.e. exit the method here from this non UI thread branch
    }
    // We are now on the UI thread ...
    UpdateData(sender);
    ...
}
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • I'm fairly certain it's the second example, i.e. OP has accidentally deleted the `}` after the return, and instead added an extra `}` after the "on UI thread" code. – StuartLC Jun 13 '18 at 06:09