2016-02-03 103 views
0

我有很多以ref/out作爲參數的方法。我讓他們這樣,因爲我有我的變量聲明在類創建方法。現在我移動到變量分開類,我不能作爲參考/輸出參數正確地傳遞變量。所以我該怎麼做呢?這些方法需要很多參數,並且在某些時候都需要它們。所以也許返回一個元組是解決方案,但我聽說返回的元組中有很多變量,它不是很好(im不知道這是否適用於.net 4.5框架)。我將發佈只是一些這裏的這些方法,因爲有超過25人..C#替代多種方法中的參數/輸出參數

private void SetPlayers(int start, int end, Panel panel, int chips, int turn, ref bool check, int newHorizontal, int newVertical, AnchorStyles one, AnchorStyles two) 
    { 
     if (chips > 0) 
     { 
      foldedPlayers--; 
      if (turn < start || turn > end) return; 
      if (Holder[start].Tag != null) 
      { 
       Holder[end].Tag = Reserve[end]; 
      } 
      Holder[start].Tag = Reserve[start]; 
      if (!check) 
      { 
       horizontal = newHorizontal; 
       vertical = newVertical; 
      } 
      check = true; 
      Holder[turn].Anchor = (one | two); 
      Holder[turn].Image = backImage; 
      //Holder[turn].Image = Deck[i]; 
      Holder[turn].Location = new Point(horizontal, vertical); 
      horizontal += Holder[turn].Width; 
      Holder[turn].Visible = true; 
      Controls.Add(panel); 
      panel.Location = new Point(Holder[start].Left - 10, Holder[start].Top - 10); 
      panel.BackColor = Color.DarkBlue; 
      panel.Height = _settings.Height + 20; 
      panel.Width = (_settings.Width + 10) * 2; 
      panel.Visible = false; 
      if (i == end) 
      { 
       check = false; 
      } 
     } 
    } 

這一條可能實際上元組來完成,我認爲:

private void Combinations(int c1, int c2, ref int current, ref int power, Control lFold) 
    { 

     for (i = 0; i < 16; i++) 
     { 
      if (Reserve[i] == int.Parse(Holder[c1].Tag.ToString()) && 
       Reserve[i + 1] == int.Parse(Holder[c2].Tag.ToString())) 
      { 
       RHighCard(ref current, ref power); 

       RPairFromTable(ref current, ref power); 

       RPairInHand(ref current, ref power); 

       RPairFromHand(ref current, ref power); 

       RTwoPairFromTable(ref current, ref power); 

       RHandTableTwoPair(ref current, ref power); 

       RTableHandTwoPair(ref current, ref power); 

       RTwoPair(ref current, ref power); 

       RThreeOfAKind(ref current, ref power, straight); 

       RStraight(ref current, ref power, straight); 

       RFlush(ref current, ref power, straight1); 

       RFullHouse(ref current, ref power, ref done, straight); 

       RFourOfAKind(ref current, ref power, straight); 

       RStraightFlush(ref current, ref power, st1, st2, st3, st4); 
      } 
     } 
    } 

我只是張貼一些的那些方法。他們都在改變功率和電流以同樣的方式:

private void RHighCard(ref int current, ref int power) 
    { 
     if (current != -1) return; 
     current = -1; 
     power = Kicker; 
     Win.Add(new Type { Power = power, Current = current }); 
    } 

private void RPairFromTable(ref int current, ref int power) 
    { 
     if (current != -1) return; 
     for (int tc = 16; tc >= 12; tc--) 
     { 
      int max = tc - 12; 
      for (int k = 1; k <= max; k++) 
      { 
       if (tc - k < 12) 
       { 
        max--; 
       } 
       if (tc - k < 12) continue; 
       if (Reserve[tc]/4 == Reserve[tc - k]/4 && Reserve[tc]/4 == 0) 
       { 
        current = 0; 
        power = 13 + Kicker; 
        Win.Add(new Type { Power = power, Current = current }); 
       } 
       else if (Reserve[tc]/4 == Reserve[tc - k]/4 && Reserve[tc]/4 != 0) 
       { 
        current = 0; 
        power = Reserve[tc]/4 + Kicker; 
        Win.Add(new Type { Power = power, Current = current }); 
       } 
      } 
     } 
    } 

我又必須通過類屬性的輸入,這些方法和它不會返回值編輯這是最主要的問題。歡迎任何替代方案。

也爲我的類變量的一些信息:

public class UsersProperties 
{ 
    public int Chips; 
    public int Type; 
    public int Power; 
    public bool Turn; 
    public bool FoldTurn; 
    public AnchorStyles CardsAnchor; 
    public readonly Panel Panel = new Panel(); 
} 

public class Player : UsersProperties 
{ 
    public Player() 
    { 
     Chips = 100000; 
     Type = -1; 
     Power = 0; 
     Turn = true; 
     FoldTurn = false; 
     CardsAnchor = AnchorStyles.Bottom; 

    } 
} 

回答

1

除了因爲你已經注意到返回一個元組,你可以通過類的實例與變量的方法,然後設置屬性或調用方法在這些類上設置方法內的變量值。

private void SetPlayers(..., MyClass myInstance) 
{ 
    .... 
    myInstance.Check = true; 
    .... 
} 

或者,也可以定義一個類或結構用get和set屬性,以保存返回的數據,然後從你的方法返回一個類的實例。

class SetPlayerData 
{ 
    public bool Check {get; set;} 
    public SetPlayerData(bool check) { this.Check = check;} 
} 

    private SetPlayerData SetPlayers(...) 
{ 
    .... 
    SetPlayerData data = new SetPlayerData(true); 
    .... 
    return data; 
} 
+0

你能提供一個示例代碼嗎? – Deadzone

+0

是否要在Player實例上設置值? – sean

+0

我真的很懷疑你的代碼真的發生了什麼 – Deadzone