C# - 创建实例

时间:2017-01-12 15:40:20

标签: c# instance

我的问题很简单,但我不知道如何解决它。

如果我已经确定创建了多少个无人机,我的代码就可以工作。

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实例创建了公共竞技场(Drone d1,Drone d2,Drone d3等等),我该如何简化?

编辑:.............

     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
        }
    }
}

3 个答案:

答案 0 :(得分:3)

如果您拥有未知数量的无人机,那么您希望使用集合类型而不是不同的字段:

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);
    }
    ...

答案 1 :(得分:0)

请在构造函数中使用'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(); 
    }
}

答案 2 :(得分:0)

您需要拥有无人机列表,因此请使用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
   }
}
相关问题