【问题标题】:C# Pass several classes as one parameterC#将多个类作为一个参数传递
【发布时间】:2016-06-28 11:34:53
【问题描述】:

我有 2 个类包含有关用户的一些信息。一个保存数据,另一个保存控制信息。现在我需要将它们都作为参数传递给方法。但是它们是相互连接的,我不认为将 2 个类作为参数单独传递是可以的。我想知道我是否应该将它们放在Tuple 或将它们保持在一起的东西中,这样我就可以将它们作为 1 个参数传递给任何方法。以下是它们的外观:

数据类

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

【问题讨论】:

  • 如果你想把它们放在一个容器类中,那就这样做吧。但是没有什么说你不应该只给两个对象作为参数。把它们放在一个元组中只会让它更复杂,不会给你任何东西

标签: c# class oop methods parameters


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-30
      • 2017-08-14
      • 2021-08-24
      • 2023-01-18
      • 2012-10-18
      • 1970-01-01
      • 2020-03-14
      • 2013-10-13
      相关资源
      最近更新 更多