2016-03-13 89 views
0

我有2個類包含有關用戶的一些信息。一個擁有數據,另一個擁有控制信息。現在我需要將它們作爲參數傳遞給方法。然而,他們彼此連接,我不認爲分開傳遞2類作爲參數是可以的。我想知道我是否應該把它們放在一個Tuple之內,或者讓它們保持在一起,這樣我就可以將它們作爲一個參數傳遞給任何方法。下面是他們的樣子:C#將多個類作爲一個參數傳遞

數據類

public class UsersProperties 
{ 
    public enum CUser 
    { 
     Player, 
     Bot1, 
     Bot2, 
     Bot3, 
     Bot4, 
     Bot5 
    } 

    public int RightCard { get; set; } 
    public string Name { get; set; } 
    public int? Chips { get; set; } 
    public int Type { get; set; } 
    public bool Turn { get; set; } 
    public bool FoldTurn { get; set; } 
    public int PreviousCall { get; set; } 
    public int LeftCard { get; set; } 
    public double Power { get; set; } 
    public int EnumCasted { get; set; } 
} 

控件類

public class UserControls 
{ 
    public Point CardsLocation { get; set; } 
    public AnchorStyles CardsAnchor { get; set; } 

    public Panel Panel { get; } = new Panel(); 
    public Point PanelLocation { get; set; } 
    public Size PanelSize { get; } = new Size((Settings.Width + 10) * 2, Settings.Height + 20); 
    public int IndentationPanelXy { get; } = 10; 

    public Label UsernameLabel { get; set; } = new Label(); 
    public Point UsernameLabelLocation { get; set; } 
    public Size UsernameLabelSize { get; } = new Size(Settings.Width * 2, 20); 

    public TextBox ChipsTextBox { get; set; } = new TextBox(); 
    public Label StatusLabel { get; set; } = new Label(); 

    public UserControls(AnchorStyles style, Point cardsLocation, bool down) 
    { 
     CardsAnchor = style; 
     CardsLocation = cardsLocation; 
     UsernameLabelLocation = down ? new Point(CardsLocation.X, CardsLocation.Y - 20) : new Point(CardsLocation.X, CardsLocation.Y + Settings.Height); 
     PanelLocation = new Point(CardsLocation.X - IndentationPanelXy, CardsLocation.Y - IndentationPanelXy); 
    } 
} 

現在,這裏是我如何對它們進行初始化:

private static Player Player = new Player(Properties.Settings.Default.StartingChips); 

private UserControls PlayerControls = new UserControls(AnchorStyles.Bottom, new Point(560, 470),false); 

而且這裏是

 private void SetPlayers(UsersProperties user, UserControls userControls, int turn, ref bool check, Image refreshbackImage) 
    { 
     if (user.Chips <= 0) return; 
     _foldedPlayers--; 
     if (turn < user.RightCard || turn > user.LeftCard) return; 
     if (Holder[user.RightCard].Tag != null) 
     { 
      Holder[user.LeftCard].Tag = _reserve[user.LeftCard]; 
     } 
     Holder[user.RightCard].Tag = _reserve[user.RightCard]; 
     if (!check) 
     { 
      _horizontal = userControls.CardsLocation.X; 
      _vertical = userControls.CardsLocation.Y; 
     } 
     check = true; 
     Holder[turn].Anchor = userControls.CardsAnchor; 
     Holder[turn].Image = refreshbackImage; 
     if (turn < Bot1.RightCard) 
     { 
      Holder[turn].Image = Deck[_i]; 
     } 
     Holder[turn].Location = new Point(_horizontal, _vertical); 
     _horizontal += Holder[turn].Width; 
     Holder[turn].Visible = true; 
     Controls.Add(userControls.Panel); 
     userControls.Panel.Location = userControls.PanelLocation; 
     userControls.Panel.BackColor = Color.DarkBlue; 
     userControls.Panel.Size = userControls.PanelSize; 
     userControls.Panel.Visible = false; 
     if (_i != user.LeftCard) return; 
     check = false; 
    } 

我想知道是否有什麼更好的方式來傳遞2類作爲參數,我想他們應該反正粘在一起:方法,其中我需要通過他們兩個。

+0

如果你想把它們放在一個容器類中,那就這樣做。但沒有什麼說你不應該只給兩個對象作爲參數。把它們放在一個元組中會讓它更復雜,不會給你任何東西 –

回答

0

您可以編寫包含兩個屬性(即usercontrol和userproperty)的包裝類(即:UserContainer)。 然後初始化它並傳遞這個類作爲參數。

相關問題