2016-01-07 37 views
0

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

  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。无论如何,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); 
    } 

正如你可以看到它说回布尔,布尔,布尔,这是我需要得到的值之后。然而,我没有很好的工作,接下来没有得到它的价值改变,如果我把return线上的断点从false下一行变为true,但一旦它退出该方法,它将恢复为默认值false。另外两个布尔值也一样。为什么?我在做什么错了如何解决这个问题。

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

回答

3

聊天谈话后编辑: 可以等待异步功能,这将给后面的元组,像这样使用:

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

,使用了我的问题第一码5次只是b1turn b1fturn等等改变b2turn b2fturn ... b5turn,b5fturn这样也你的代码会让我不能等待bool – kopelence

+0

我不明白到底发生了什么和你尝试设置断点。你可以清理吗? – NikxDa

+0

林不知道你不明白。接下来必须拿出真实的价值。 – kopelence