2014-10-20 124 views
-1

后空。控制标签成为铸造

public partial class SomethingAllocator : UserControl 
{ 
    GlobalOptions g = Program.Core.GlobalOptions; 
    public SomethingAllocator() 
    { 
     InitializeComponent(); 

    } 

    private void SomethingAllocator_Load(object sender, EventArgs e) 
    { 
     ucsAlloc1.lbl_Name.Text = "S 1 - Primary"; 
     ucsAlloc2.lbl_Name.Text = "S 2"; 
     ucsAlloc3.lbl_Name.Text = "S 3"; 
     ucsAlloc4.lbl_Name.Text = "S 4"; 
     ucsAlloc5.lbl_Name.Text = "S 5"; 
     ucsAlloc6.lbl_Name.Text = "S 6"; 


     ucsAlloc1.Tag = 0; 
     ucsAlloc2.Tag = 1; 
     ucsAlloc3.Tag = 2; 
     ucsAlloc4.Tag = 3; 
     ucsAlloc5.Tag = 4; 
     ucsAlloc6.Tag = 5; 

     ucsAlloc1.cbx_DL.SelectedIndex = 3; 
     ucsAlloc1.cbx_UL.SelectedIndex = 3; 
     ucsAlloc2.cbx_DL.SelectedIndex = 3; 
     ucsAlloc2.cbx_UL.SelectedIndex = 3; 
     ucsAlloc3.cbx_DL.SelectedIndex = 3; 
     ucsAlloc3.cbx_UL.SelectedIndex = 3; 
     ucsAlloc4.cbx_DL.SelectedIndex = 3; 
     ucsAlloc4.cbx_UL.SelectedIndex = 3; 
     ucsAlloc5.cbx_DL.SelectedIndex = 3; 
     ucsAlloc5.cbx_UL.SelectedIndex = 3; 
     ucsAlloc6.cbx_DL.SelectedIndex = 3; 
     ucsAlloc6.cbx_UL.SelectedIndex = 3; 
     InitSs(); 



    } 

    void ucsAlloc1_onComboIndexChanged(object sender, EventArgs e) 
    { 
     var cont = ((sender as Control).Parent as UCSAlloc); 
     var selected = cont.comboBox1.SelectedIndex; 
     if (selected == 0 && (int)cont.Tag == 0) 
     { 
      cont.comboBox1.SelectedIndex = cont.comboBox1.Items.IndexOf(Program.Core.Mission.AllocatedStations[0].Name); 
      MessageBox.Show("****"); 
      return; 
     } 
     if (selected == 0) 
     { 
      Program.Core.Mission.AllocatedStations[(int)cont.Tag] = null; 
      return; 
     } 


     foreach (var item in this.Controls.OfType<UCSAlloc>()) 
     { 
      if (item != cont) 
      { 
       if (item.comboBox1.SelectedIndex == selected) 
       { 
        cont.comboBox1.SelectedIndex = 0; 
        MessageBox.Show("***"); 
        return; 
       } 
      } 
     } 
     var station = g.Stations.FirstOrDefault(x => x.Name == cont.comboBox1.Text); 
     if (station == null) 
      MessageBox.Show("***"); 
     else 
     { 
      var index = (int)cont.Tag; 
      Program.Core.Mission.AllocatedStations[index] = station; 
      cont.cbx_DL.SelectedIndex = cont.cbx_DL.Items.IndexOf(Program.Core.Mission.AllocatedStations[index].DlChannel.ToString()); 
      cont.cbx_UL.SelectedIndex = cont.cbx_UL.Items.IndexOf(Program.Core.Mission.AllocatedStations[index].UlChannel.ToString()); 
     } 
    } 

    public void InitSs() 
    { 
     g = Program.Core.GlobalOptions; 
     ucsAlloc1.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged; 
     ucsAlloc2.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged; 
     ucsAlloc3.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged; 
     ucsAlloc4.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged; 
     ucsAlloc5.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged; 
     ucsAlloc6.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged; 

     ucsAlloc1.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged; 
     ucsAlloc2.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged; 
     ucsAlloc3.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged; 
     ucsAlloc4.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged; 
     ucsAlloc5.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged; 
     ucsAlloc6.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged; 


     ucsAlloc1.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged; 
     ucsAlloc2.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged; 
     ucsAlloc3.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged; 
     ucsAlloc4.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged; 
     ucsAlloc5.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged; 
     ucsAlloc6.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged; 


     ucgsAlloc1.comboBox1.Items.Clear(); 
     ucgsAlloc2.comboBox1.Items.Clear(); 
     ucgsAlloc3.comboBox1.Items.Clear(); 
     ucgsAlloc4.comboBox1.Items.Clear(); 
     ucgsAlloc5.comboBox1.Items.Clear(); 
     ucgsAlloc6.comboBox1.Items.Clear(); 

     ucsAlloc1.comboBox1.Items.Add("None"); 
     ucsAlloc2.comboBox1.Items.Add("None"); 
     ucsAlloc3.comboBox1.Items.Add("None"); 
     ucsAlloc4.comboBox1.Items.Add("None"); 
     ucsAlloc5.comboBox1.Items.Add("None"); 
     ucsAlloc6.comboBox1.Items.Add("None"); 

     for (int i = 0; i < g.Stations.Count; i++) 
     { 
      var name = g.Stations[i].Name; 
      ucsAlloc1.comboBox1.Items.Add(name); 
      ucsAlloc2.comboBox1.Items.Add(name); 
      ucsAlloc3.comboBox1.Items.Add(name); 
      ucsAlloc4.comboBox1.Items.Add(name); 
      ucsAlloc5.comboBox1.Items.Add(name); 
      ucsAlloc6.comboBox1.Items.Add(name); 
     } 

     ucsAlloc1.comboBox1.SelectedIndex = 0; 
     ucsAlloc2.comboBox1.SelectedIndex = 0; 
     ucsAlloc3.comboBox1.SelectedIndex = 0; 
     ucsAlloc4.comboBox1.SelectedIndex = 0; 
     ucsAlloc5.comboBox1.SelectedIndex = 0; 
     ucsAlloc6.comboBox1.SelectedIndex = 0; 

     if (g.Stations.Count > 0) 
      ucsAlloc1.comboBox1.SelectedIndex = 1; 
     if (Program.Core.Mission != null) 
     { 
      for (int i = 0; i < Program.Core.Mission.AllocatedStations.Length; i++) 
      { 
       if (Program.Core.Mission.AllocatedStations[i] == null) continue; 
       var loc = this.Controls.OfType < UCSAlloc>().FirstOrDefault(t => (int)t.Tag == i);//--------------null exception over the tag property 
       if (loc != null) 
       { 
        var item = Program.Core.Mission.AllocatedStations[i]; 
        var nameIndex = loc.comboBox1.Items.IndexOf(item.Name.ToString()); 
        if (nameIndex > -1) 
         loc.comboBox1.SelectedIndex = nameIndex; 
        loc.cbx_DL.SelectedIndex = loc.cbx_DL.Items.IndexOf(item.DlChannel.ToString()); 
        loc.cbx_UL.SelectedIndex = loc.cbx_UL.Items.IndexOf(item.UlChannel.ToString()); 
       } 
      } 
     } 


     ucsAlloc1.onComboIndexChanged += ucsAlloc1_onComboIndexChanged; 
     ucsAlloc2.onComboIndexChanged += ucsAlloc1_onComboIndexChanged; 
     ucsAlloc3.onComboIndexChanged += ucsAlloc1_onComboIndexChanged; 
     ucsAlloc4.onComboIndexChanged += ucsAlloc1_onComboIndexChanged; 
     ucsAlloc5.onComboIndexChanged += ucsAlloc1_onComboIndexChanged; 
     ucsAlloc6.onComboIndexChanged += ucsAlloc1_onComboIndexChanged; 

     ucsAlloc1.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged; 
     ucsAlloc2.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged; 
     ucsAlloc3.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged; 
     ucsAlloc4.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged; 
     ucsAlloc5.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged; 
     ucsAlloc6.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged; 


     ucsAlloc1.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged; 
     ucsAlloc2.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged; 
     ucsAlloc3.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged; 
     ucsAlloc4.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged; 
     ucsAlloc5.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged; 
     ucsAlloc6.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged; 

    } 

    void cbx_UL_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     var ctrl = (sender as Control).Parent as UCSAlloc;   
     var number = ctrl.cbx_UL.Text; 
     int chnl; 
     if (int.TryParse(number, out chnl)) 
     { 
      if (Program.Core.Mission.AllocatedStations[(int)ctrl.Tag] != null)////-------- null exception over tag. ctrl isnt null 
       Program.Core.Mission.AllocatedStations[(int)ctrl.Tag].UlChannel = chnl; 
     } 



    } 

    void cbx_DL_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     var ctrl = (sender as Control).Parent as UCSAlloc;   
     var number = ctrl.cbx_DL.Text; 
     int chnl; 
     if (int.TryParse(number, out chnl)) 
     { 
      if (Program.Core.Mission.AllocatedStations[(int)ctrl.Tag] != null)////-------- null exception over tag. ctrl isnt null 
       Program.Core.Mission.AllocatedStations[(int)ctrl.Tag].DlChannel = chnl; 
     } 
    } 
} 
+2

你的第二个片段甚至不进行编译,因为'object'没有一个'Tag'属性。如果您仅提供不能证明您所声称的行为的代码,我们无法辨别您的* real *代码有什么问题。 – 2014-10-20 08:59:15

+0

显示您的标签初始化 – thumbmunkeys 2014-10-20 09:00:00

+0

非常丰富,可能重复http://stackoverflow.com/questions/4926677/c-sharp-as-cast-vs-classic-cast – 2014-10-20 09:08:11

回答

1

条件在Where是错误的:

.Where(item=>item.Tag==123) 

默认情况下,引用类型,该==运营商的测试参考平等。由于item.TagObject类型的,item.Tag == 123测试是否item.Tag123是相同的对象实例;但由于int是值类型,123盒装到一个新的对象实例,所以这是从来没有相同的实例作为item.Tag。相反,你应该使用Equals方法:

.Where(item=>item.Tag.Equals(123)) 

不像==运算符,Equals方法是虚拟的,所以不同的实现将根据其所被称为实例中使用;在这种情况下,它将调用Int32.Equals方法,如果标记为123,则该方法将正确返回true。


编辑:你的问题如下编辑

的问题是在这里:

FirstOrDefault(t => (int)t.Tag == i) 

如果t.Tag为空,投值类型抛出一个NullReferenceException。检查您确实初始化了UCSAlloc的所有实例的

+0

我很抱歉,我解析Tag到int的值。我猜这个问题我写得很严厉。 – 2014-10-20 09:15:00

0

“crtl”本身在转换失败(无论什么原因)时为空,因为如果转换失败,则使用“as”投射将返回null。

+0

是真的,但铸造不会在这里失败。 我在演员阵容后检查了这个项目并且没问题。 Tag属性以及其他一些属性为null – 2014-10-20 09:17:18