【发布时间】: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