Call is ambiguous error - Dice Game

时间:2016-07-11 20:27:54

标签: c# wpf methods error-handling

Hi i'm trying to make a dice game and keep getting this error, not sure what it is or if there is something wrong with my algorithm. Any help would be appreciated. Thanks :) It shows an error:

The call is ambiguous between the following methods or properties: 'Form1.UpdateGameStatus(int)' and 'Form1.UpdateGameStatus(int)'

    private int UpdateGameStatus(int sumDice)
    {

        if (sumDice == 2)
        if (sumDice == 3)
        if (sumDice == 14) 
            lblSum.Text = ("You lost!");
        if (sumDice == 9)
        if (sumDice == 10)
            lblSum.Text = ("You're winner!");
        if (sumDice == 0)
            lblSum.Text = ("Roll again please!");
        return; 

    }

    int point = 0;
    int gamestatus = 0;
    private void UpdateGameStatus(int sumDice)
    {
        while (sumDice == 4 || sumDice == 5 || sumDice == 6 || sumDice==||sumDice == 9 || sumDice == 10)

            point = sumDice;

        if (sumDice == point)
            lblSum.Text = ("You're winner!");
        gamestatus = 1;
        if (sumDice == 7)
            lblSum.Text = ("You lost!");
        gamestatus = -1;
        return;
    }
    private void btnRollDice_Click(object sender, EventArgs e)
    {

        int die1 = 0, die2 = 0;
        RollDice(out die1, out die2);
        UpdateGameStatus(sumDice);     
   }   
}
}      

2 个答案:

答案 0 :(得分:3)

You have two methods with the same signature:

private void UpdateGameStatus(int sumDice)

and

private int UpdateGameStatus(int sumDice)

The compiler can't know which one you mean. They have different return types, but those are not part of the signature.

答案 1 :(得分:0)

The error message tells you what's wrong: you are making an ambiguous call to UpdateGameStatus. You have two methods, both named UpdateGameStatus that both take an integer value. Why don't you just have one version that returns the sum of the dice and also sets the game status? That would solve your problem. You're duplicating code unnecessarily here.

Edit: I also noticed that your first method doesn't actually update the game status at all, so if you do want to keep both, consider renaming that one.

Edit 2: To summarize what was mentioned in the comments (and to go beyond just the error message), you seem to have some unnecessary code duplication. Think about what you really want this method to do. Do you want it to set the text to "you lose!"? Do you want it to track the gamestatus variable (1, 0, or -1)? Do you want it to return the gamestatus variable? Figure out exactly what you want this method to do/return and try to remove the duplicated code.