C#将多个类作为一个参数传递
我有两个类,其中包含一些关于用户的信息。一个保存数据,另一个保存控件信息。现在我需要将它们作为参数传递给一个方法。但是它们是相互连接的,我不认为将这两个类分别作为参数传递是可以的。我想知道是否应该将它们放在一个C#将多个类作为一个参数传递,c#,class,oop,methods,parameters,C#,Class,Oop,Methods,Parameters,我有两个类,其中包含一些关于用户的信息。一个保存数据,另一个保存控件信息。现在我需要将它们作为参数传递给一个方法。但是它们是相互连接的,我不认为将这两个类分别作为参数传递是可以的。我想知道是否应该将它们放在一个元组中或者将它们放在一起的东西中,这样我就可以将它们作为一个参数传递给任何方法。以下是它们的外观: 数据类: public class UsersProperties { public enum CUser { Player, Bot1,
元组中
或者将它们放在一起的东西中,这样我就可以将它们作为一个参数传递给任何方法。以下是它们的外观:
数据类:
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);
}
}
控件类:
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;
}
private void SetPlayers(usersprroperties用户、UserControls用户控件、int-turn、ref-bool检查、图像刷新backimage)
{
如果(user.Chips user.LeftCard)返回;
if(持有者[user.RightCard].Tag!=null)
{
持有者[user.LeftCard].Tag=_reserve[user.LeftCard];
}
持有者[user.RightCard].Tag=_reserve[user.RightCard];
如果(!检查)
{
_水平=userControls.CardsLocation.X;
_垂直=userControls.CardsLocation.Y;
}
检查=正确;
Holder[turn].Anchor=userControls.CardsAnchor;
保持架[转动]。图像=刷新背面图像;
如果(转动<1.右卡)
{
保持架[转动]。图像=甲板[_i];
}
保持架[转动]。位置=新点(_水平,_垂直);
_水平+=支架[转动]。宽度;
保持架[转动]。可见=真;
添加(userControls.Panel);
userControls.Panel.Location=userControls.PanelLocation;
userControls.Panel.BackColor=Color.DarkBlue;
userControls.Panel.Size=userControls.PanelSize;
userControls.Panel.Visible=false;
如果(_i!=user.LeftCard)返回;
检查=错误;
}
我想知道是否有更好的方法将两个类作为参数传递,我认为它们应该粘在一起。您可以编写一个包装类(即:UserContainer),它有两个属性,即usercontrol和userproperty。
然后初始化它并将该类作为参数传递。我认为您可以使用泛型,如下所示:
namespace PropertiesControls
{
public class PropsControls
{
public static void PropesAndControls<TP, TC>(TP property, TC control) where TP : UserProperties where TC: UserControls
{
//your code here
}
}
}
命名空间属性控件
{
公共类props控件
{
公共静态void属性和控件(TP属性,TC控件),其中TP:UserProperties,其中TC:UserControls
{
//你的代码在这里
}
}
}
泛型定义可应用于类的行为。它用于集合中,在集合中,无论对象的类型如何,都可以应用相同的方法来处理对象。也就是说,可以使用相同的逻辑处理字符串列表或整数列表,而无需区分这两种特定类型。如果要将它们放入容器类中,请执行此操作。但是没有什么规定你不应该只给出两个对象作为参数。把它们放进元组只会让事情变得更复杂,不会给你任何东西