C#将几个类作为一个参数传递

时间:2016-03-13 12:49:13

标签: c# class oop methods parameters

我有2个类,其中包含有关用户的一些信息。一个保存数据,另一个保存控制信息。现在我需要将它们作为参数传递给方法。然而,他们彼此连接,我不认为将2个类分别作为参数传递是可以的。我想知道我是否应该将它们放在Tuple中或者将它们放在一起的东西中,这样我就可以将它们作为任何方法的一个参数传递给我。这是他们看起来的样子:

数据类

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个类作为参数传递,我认为它们应该粘在一起。

2 个答案:

答案 0 :(得分:0)

您可以编写一个包装类(即:UserContainer),它有两个属性,即usercontrol和userproperty。 然后初始化它并将此类作为参数传递。

答案 1 :(得分:0)

我认为您可以使用泛型,如下所示:

namespace PropertiesControls
{
    public class PropsControls
    {
        public static void PropesAndControls<TP, TC>(TP property, TC control) where TP : UserProperties where TC: UserControls
        {
            //your code here
        }
    }
}

泛型定义了可以应用于类的行为。它在集合中使用,在集合中,无论对象的类型如何,都可以使用相同的方法来处理对象。也就是说,可以使用相同的逻辑处理字符串列表或整数列表,而不必区分这两种特定类型。

相关问题