2017-01-12 15 views
0

私の問題はかなりシンプルですが、解決方法はわかりません。C# - インスタンスの作成

作成されたドロンズの数が既に決定されている場合、私のコードは動作します。

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にはどのコンボボックスが無人機であるかをチェックする必要があります。コードのこの部分で

private Drone d1, d2; 

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


    } 

:例えばドローンの10件のインスタンスが存在する場合、パブリックアリーナ(ドローンのD1、D2ドローン、ドローン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 
     } 
    } 
} 
+5

別の 'dN'フィールドの代わりに' List​​'を使用してください。 –

+1

@DStanleyあなたは恐らくそれを働かせるかもしれない答え – cubrr

+0

hmに書きたいと思うでしょう。 Uは少し良く説明できますか? d1、d2などを作成するのではなく、それらをすべてリストに入れ、公開アリーナ(List <> drones) –

答えて

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

またはあなたが無人偵察機のリストを持っているので、List<Drone>を使用する必要がありますどのように多くのそれらの


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

thxで使用します。私の編集を確認してください。無人機のIPがコンボボックスにあるかどうかを確認するにはどうすればよいですか? –

+0

リストの無人機を検索するときにlinqを使用しようとします。 list.Where(x => x.ip_drone == combobox.selected) – mybrave

0

わからない場合。次に、あなたの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(); 
    } 
} 

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

thxxxxxxxxxxxxx –

+0

私は助けてくれると嬉しいです。 – CodingYoshi

関連する問題