简体   繁体   English

C#-创建实例

[英]C# - Creating Instances

My problem is quite simple but I dont know how to solve it. 我的问题很简单,但我不知道如何解决。

My code works if I already determine how many Drones are created. 如果我已经确定要创建多少个无人机,我的代码将起作用。

INICIO 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(); 
    }


}

Arena: 竞技场:

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

My question is: How can I do this for n drones? 我的问题是:如何为n架无人机做到这一点? Everytime I click a button it will add a new drone (d3,d4,d5,etc...) and on ARENA I need to check which combobox selected item is that drone's ip. 每当我单击一个按钮时,它将添加一个新的无人机(d3,d4,d5等),在ARENA上,我需要检查哪个组合框选择项是该无人机的ip。

 private Drone d1, d2;

    public Arena(Drone d1,Drone d2)
    {
        InitializeComponent();
        this.d1 = d1;
        this.d2 = d2;


    }

in this part of the code: for example if there are 10 instances of Drone created public Arena(Drone d1,Drone d2,Drone d3,etc...) how can I simplify this? 在这部分代码中:例如,如果有10个Drone创建了公共Arena(Drone d1,Drone d2,Drone d3等),我该如何简化呢?

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

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

If you have an unknown number of drones then you want to use a collection type instead of distinct fields: 如果您有未知数量的无人机,则要使用集合类型而不是不同的字段:

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

Please use 'params' keyword in constructor: 请在构造函数中使用“ 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
            }
        }
    }
}

This way you can use it like this (when you already know amount of them) 这样,您就可以这样使用它(当您已经知道它们的数量时)


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

or if you don't know how many of them 或者如果您不知道其中有多少


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

You need to have a list of drones so use List<Drone> . 您需要拥有无人机列表,因此请使用List<Drone> Then pass that list to your Arena : 然后将该列表传递给您的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();
   }
}

In your Arena set the combobox's datasource to the the List<Drone> . 在您的Arena将组合框的数据源设置为List<Drone> When the combobox is changed, you can get the SelectedItem and it will have the drone object selected. 更改组合框后,您可以获取SelectedItem ,它将选择无人机对象。 I am also showing how to get the other values in the code if you need them. 如果需要,我还将展示如何在代码中获取其他值。 You should not need to loop and search for the selected item. 您无需循环并搜索所选项目。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM