【问题标题】:C# Tuple not returning valueC#元组不返回值
【发布时间】:2016-04-12 09:21:44
【问题描述】:

这是我第一次使用元组,所以我可能会做一些完全错误的事情,或者只是错过了一些小事情。我曾经有重复的代码:

            if (!B1Fturn)
            {
                if (B1turn)
                {
                    FixCall(b1Status, ref b1Call, ref b1Raise, 1);
                    FixCall(b1Status, ref b1Call, ref b1Raise, 2);
                    Rules(2, 3, "Bot 1", ref b1Type, ref b1Power, B1Fturn, b1Status);
                    AutoCloseMsb.Show("Bot 1 Turn", "Turns", thinkTime);
                    AI(2, 3, ref bot1Chips, ref B1turn, ref  B1Fturn, b1Status, b1Power, b1Type);
                    turnCount++;
                    B1turn = false;
                    B2turn = true;
                }
            }
            if (B1Fturn && !b1Folded)
            {
                bools.RemoveAt(1);
                bools.Insert(1, null);
                maxLeft--;
                b1Folded = true;
            }
            if (B1Fturn || !B1turn)
            {
                await CheckRaise(1, 1);
                B2turn = true;
            }

我复制并粘贴了 5 次,所以我决定将它们全部放在一个方法中,然后将方法粘贴 5 次。所以我开始这样做,但我意识到我需要等待一些方法,所以我选择了async Task<some input> 但是我需要取回一些值,一些布尔值和 int 的 .2 选项要么返回要么使用 ref/out。无论如何,异步方法中都不允许引用/输出,因此只剩下返回。问题是我需要返回几种不同类型的数据,所以我需要一些可以做到这一点的东西(它是元组)。我使用 Tuple 创建了一种方法:

async Task<Tuple<bool, bool, bool>> Rotating(bool tempTurn, bool permaTurn, bool folded, string name, Label Status, int botCall, int botRaise, int start, int end, int current, bool next, double power, double type, int chips)
    {
        if (!permaTurn)
        {
            if (tempTurn)
            {
                FixCall(Status, ref botCall, ref botRaise, current);
                FixCall(Status, ref botCall, ref botRaise, start);
                Rules(start, end, name, ref type, ref power, permaTurn,Status);
                AutoCloseMsb.Show(name + " Turn", "Turns", thinkTime);
                AI(start, end, ref chips, ref tempTurn, ref  permaTurn, Status, power, type);
                turnCount++;
                tempTurn = false;
                next = true;
            }
        }
        if (permaTurn && !folded)
        {
            bools.RemoveAt(current);
            bools.Insert(current, null);
            maxLeft--;
            folded = true;
        }
        if (permaTurn || !tempTurn)
        {
            await CheckRaise(current, current);
            next = true;
        }
        return new Tuple<bool, bool, bool>(tempTurn, permaTurn, next);
    }

如您所见,它说返回 bool、bool、bool,然后是我需要获取的值。但是,如果我在 return 行上放置断点,从 false next 变为 true,我并没有很好地解决它的值,但是一旦它退出该方法,它就会恢复为默认值 false 。其他 2 个布尔值也是如此。为什么 ?我在做什么错如何解决这个问题。

  • P.S 我还需要返回一些我之前提到的 int,但为了简单起见,我删除了它们。

【问题讨论】:

    标签: c# asynchronous return tuples task


    【解决方案1】:

    在聊天对话后编辑: 您可以等待将返回元组的异步函数,像这样使用它:

    var r = await Test (x, y); // x,y for example, Test returns Tuple
    Now you can access r.Item1 and r.Item2
    

    【讨论】:

    • 我的问题中的第一个代码被使用了 5 次,只是 b1turn b1fturn 等被 b2turn b2fturn 改变... b5turn,b5fturn 像这样你的代码也会给我不可等待的 bool
    • 我不明白接下来会发生什么以及您尝试设置断点的方式。可以澄清一下吗?
    • 我不确定你不明白什么。 Next 的值必须为 true。
    • 是的,但要做到这一点,permaTurn || !tempTurn 必须为真。我看不到任何实际的函数调用,所以我不能说它是否曾经是。我有什么误解吗?
    • 我看不出有什么理由可以改变这种情况,但无论如何它总会被证明是真的
    猜你喜欢
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多