C# - 创建实例
C# - Creating Instances
我的问题很简单,但我不知道如何解决。
如果我已经确定了创建了多少无人机,我的代码就可以工作。
INICIO
public partial class Inicio : Form
{
private Drone d1,d2;
private Arena arena;
public Inicio()
{
InitializeComponent();
}
private void btnconetar_Click(object sender, EventArgs e)
{
d1 = new Drone("192.168.1.10");
d2 = new Drone("192.168.1.20");
arena = new Arena(d1,d2);
arena.Show();
this.Hide();
}
}
竞技场:
public partial class Arena : Form
{
private Drone d1, d2;
public Arena(Drone d1,Drone d2)
{
InitializeComponent();
this.d1 = d1;
this.d2 = d2;
}
private void cb_drone_SelectedIndexChanged(object sender, EventArgs e)
{
if(cb_drone.SelectedIndex.ToString() == d1.ip_drone)
{
//do something
}
}
}
我的问题是:如何为 n 架无人机执行此操作?每次我单击一个按钮时,它都会添加一个新的无人机(d3、d4、d5 等...),在 ARENA 上我需要检查哪个组合框选择的项目是该无人机的 ip。
private Drone d1, d2;
public Arena(Drone d1,Drone d2)
{
InitializeComponent();
this.d1 = d1;
this.d2 = d2;
}
在这部分代码中:例如,如果创建了 10 个 Drone 实例 public Arena(Drone d1,Drone d2,Drone d3,etc...) 我该如何简化它?
编辑:......
List<Drone> lista_drones = new List<Drone>;
private Arena arena;
public Inicio()
{
InitializeComponent();
}
private void Inicio_Load(object sender, EventArgs e)
{
}
private void btnconetar_Click(object sender, EventArgs e)
{
lista_drones.Add(new Drone("192.168.1.10"));
lista_drones.Add(new Drone("192.168.1.20"));
arena = new Arena(lista_drones);
arena.Show();
this.Hide();
}
public partial class Arena : Form
{
public Arena(List<Drone> lista_drones)
{
InitializeComponent();
}
private void cb_drone_SelectedIndexChanged(object sender, EventArgs e)
{
if(cb_drone.SelectedIndex.ToString() == )
{
//do something
}
}
}
如果您的无人机数量未知,那么您想使用集合类型而不是不同的字段:
public partial class Inicio : Form
{
private List<Drone> drones;
private Arena arena;
...
public partial class Arena : Form
{
private List<Drone> drones;
public Arena(IEnumerable<Drone> drones)
{
InitializeComponent();
drones = new List<Drone>(drones);
}
...
请在构造函数中使用'params'关键字:
public partial class Arena : Form
{
private readonly Drone[] d;
public Arena(params Drone[] d)
{
InitializeComponent();
this.d = d;
}
private void cb_drone_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (var di in this.d)
{
if(cb_drone.SelectedIndex.ToString() == di.ip_drone)
{
//do something
}
}
}
}
这样你就可以像这样使用它(当你已经知道它们的数量时)
public partial class Inicio : Form
{
private Drone d1,d2;
private Arena arena;
public Inicio()
{
InitializeComponent();
}
private void btnconetar_Click(object sender, EventArgs e)
{
d1 = new Drone("192.168.1.10");
d2 = new Drone("192.168.1.20");
....
dn = new Drone("192.168.1.xx");
arena = new Arena(d1,d2);
arena.Show();
this.Hide();
}
}
或者如果你不知道其中有多少
public partial class Inicio : Form
{
private List d;
private Arena arena;
public Inicio()
{
InitializeComponent();
}
private void btnconetar_Click(object sender, EventArgs e)
{
d = new List(){ new Drone("192.168.1.10"), /* whatever */ };
arena = new Arena(d.ToArray());
arena.Show();
this.Hide();
}
}
您需要一份无人机列表,因此请使用 List<Drone>
。然后将该列表传递给您的 Arena
:
public partial class Inicio : Form {
private List<Drone> drones;
private Arena arena;
public Inicio() {
InitializeComponent();
this.drones = new List<Drone>();
}
private void btnconetar_Click(object sender, EventArgs e) {
d1 = new Drone( "192.168.1.10" );
d2 = new Drone( "192.168.1.20" );
drones.Add( d1 );
drones.Add( d2 );
// more drones
arena = new Arena( drones );
arena.Show();
this.Hide();
}
}
在您的 Arena
中,将组合框的数据源设置为 List<Drone>
。更改组合框时,您可以获得 SelectedItem
并且它将选择无人机对象。如果需要,我还会展示如何在代码中获取其他值。您应该不需要循环和搜索所选项目。
public partial class Arena : Form {
private List<Drone> drones;
public Arena(List<Drone> drones) {
InitializeComponent();
this.drones = drones;
cb_drone.DataSource = drones;
// This should be whatever the property name is in your drone class
cb_drones.ValueMember = "DroneIp";
// THis should be whatever the property name is
// in your drone class that you want to display to the user
cb_drones.DisplayMember = "DroneSomething";
}
private void cb_drone_SelectedIndexChanged(object sender, EventArgs e) {
// will give you the drone object
var selectedDrone = cb_drone.SelectedItem;
// var value = cb_drone.SelectedValue; will give you the Ip (whatever you specified in ValueMember)
// var selectedDrone = this.drones.Where(x => x.DroneIp == cb_drone.SelectedValue)
//do something with selectedDrone or the other things
}
}
我的问题很简单,但我不知道如何解决。
如果我已经确定了创建了多少无人机,我的代码就可以工作。
INICIO
public partial class Inicio : Form
{
private Drone d1,d2;
private Arena arena;
public Inicio()
{
InitializeComponent();
}
private void btnconetar_Click(object sender, EventArgs e)
{
d1 = new Drone("192.168.1.10");
d2 = new Drone("192.168.1.20");
arena = new Arena(d1,d2);
arena.Show();
this.Hide();
}
}
竞技场:
public partial class Arena : Form
{
private Drone d1, d2;
public Arena(Drone d1,Drone d2)
{
InitializeComponent();
this.d1 = d1;
this.d2 = d2;
}
private void cb_drone_SelectedIndexChanged(object sender, EventArgs e)
{
if(cb_drone.SelectedIndex.ToString() == d1.ip_drone)
{
//do something
}
}
}
我的问题是:如何为 n 架无人机执行此操作?每次我单击一个按钮时,它都会添加一个新的无人机(d3、d4、d5 等...),在 ARENA 上我需要检查哪个组合框选择的项目是该无人机的 ip。
private Drone d1, d2;
public Arena(Drone d1,Drone d2)
{
InitializeComponent();
this.d1 = d1;
this.d2 = d2;
}
在这部分代码中:例如,如果创建了 10 个 Drone 实例 public Arena(Drone d1,Drone d2,Drone d3,etc...) 我该如何简化它?
编辑:......
List<Drone> lista_drones = new List<Drone>;
private Arena arena;
public Inicio()
{
InitializeComponent();
}
private void Inicio_Load(object sender, EventArgs e)
{
}
private void btnconetar_Click(object sender, EventArgs e)
{
lista_drones.Add(new Drone("192.168.1.10"));
lista_drones.Add(new Drone("192.168.1.20"));
arena = new Arena(lista_drones);
arena.Show();
this.Hide();
}
public partial class Arena : Form
{
public Arena(List<Drone> lista_drones)
{
InitializeComponent();
}
private void cb_drone_SelectedIndexChanged(object sender, EventArgs e)
{
if(cb_drone.SelectedIndex.ToString() == )
{
//do something
}
}
}
如果您的无人机数量未知,那么您想使用集合类型而不是不同的字段:
public partial class Inicio : Form
{
private List<Drone> drones;
private Arena arena;
...
public partial class Arena : Form
{
private List<Drone> drones;
public Arena(IEnumerable<Drone> drones)
{
InitializeComponent();
drones = new List<Drone>(drones);
}
...
请在构造函数中使用'params'关键字:
public partial class Arena : Form
{
private readonly Drone[] d;
public Arena(params Drone[] d)
{
InitializeComponent();
this.d = d;
}
private void cb_drone_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (var di in this.d)
{
if(cb_drone.SelectedIndex.ToString() == di.ip_drone)
{
//do something
}
}
}
}
这样你就可以像这样使用它(当你已经知道它们的数量时)
public partial class Inicio : Form
{
private Drone d1,d2;
private Arena arena;
public Inicio()
{
InitializeComponent();
}
private void btnconetar_Click(object sender, EventArgs e)
{
d1 = new Drone("192.168.1.10");
d2 = new Drone("192.168.1.20");
....
dn = new Drone("192.168.1.xx");
arena = new Arena(d1,d2);
arena.Show();
this.Hide();
}
}
或者如果你不知道其中有多少
public partial class Inicio : Form
{
private List d;
private Arena arena;
public Inicio()
{
InitializeComponent();
}
private void btnconetar_Click(object sender, EventArgs e)
{
d = new List(){ new Drone("192.168.1.10"), /* whatever */ };
arena = new Arena(d.ToArray());
arena.Show();
this.Hide();
}
}
您需要一份无人机列表,因此请使用 List<Drone>
。然后将该列表传递给您的 Arena
:
public partial class Inicio : Form {
private List<Drone> drones;
private Arena arena;
public Inicio() {
InitializeComponent();
this.drones = new List<Drone>();
}
private void btnconetar_Click(object sender, EventArgs e) {
d1 = new Drone( "192.168.1.10" );
d2 = new Drone( "192.168.1.20" );
drones.Add( d1 );
drones.Add( d2 );
// more drones
arena = new Arena( drones );
arena.Show();
this.Hide();
}
}
在您的 Arena
中,将组合框的数据源设置为 List<Drone>
。更改组合框时,您可以获得 SelectedItem
并且它将选择无人机对象。如果需要,我还会展示如何在代码中获取其他值。您应该不需要循环和搜索所选项目。
public partial class Arena : Form {
private List<Drone> drones;
public Arena(List<Drone> drones) {
InitializeComponent();
this.drones = drones;
cb_drone.DataSource = drones;
// This should be whatever the property name is in your drone class
cb_drones.ValueMember = "DroneIp";
// THis should be whatever the property name is
// in your drone class that you want to display to the user
cb_drones.DisplayMember = "DroneSomething";
}
private void cb_drone_SelectedIndexChanged(object sender, EventArgs e) {
// will give you the drone object
var selectedDrone = cb_drone.SelectedItem;
// var value = cb_drone.SelectedValue; will give you the Ip (whatever you specified in ValueMember)
// var selectedDrone = this.drones.Where(x => x.DroneIp == cb_drone.SelectedValue)
//do something with selectedDrone or the other things
}
}