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;
}
}
你能提供一個示例代碼嗎? – Deadzone
是否要在Player實例上設置值? – sean
我真的很懷疑你的代碼真的發生了什麼 – Deadzone