[英]control tag becomes null after casting
when i cast a control to its type the Tag
property of the resulted item is null while the Tag
property of the original control is unchanged. 当我投了控制其类型的
Tag
所得到的物品属性为空,而Tag
的原控制的属性是不变的。
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;
}
}
}
The condition in your Where
is wrong: 在你的情况
Where
是错误的:
.Where(item=>item.Tag==123)
By default, for reference types, the ==
operator tests reference equality. 默认情况下,对于引用类型,
==
运算符将测试引用相等性。 Since item.Tag
is of type Object
, item.Tag == 123
tests whether item.Tag
and 123
are the same object instance; 由于
item.Tag
的类型为Object
,因此item.Tag == 123
测试item.Tag
和123
是否为同一对象实例; but since int
is a value type, 123
is boxed into a new object instance , so it's never the same instance as item.Tag
. 但由于
int
是一种值类型,因此将123
装箱到新的对象实例中 ,因此它永远不会与item.Tag
相同。 Instead you should use the Equals
method: 相反,您应该使用
Equals
方法:
.Where(item=>item.Tag.Equals(123))
Unlike the ==
operator, the Equals
method is virtual, so a different implementation will be used depending on the instance on which it is called; 与
==
运算符不同, Equals
方法是虚拟的,因此将根据调用该实例的实例使用不同的实现。 in this case it will call the Int32.Equals
method, which will correctly return true if the tag is 123
. 在这种情况下,它将调用
Int32.Equals
方法,如果标记为123
则该方法将正确返回true。
EDIT: following the edit in your question 编辑:按照您的问题进行编辑
The problem is here: 问题在这里:
FirstOrDefault(t => (int)t.Tag == i)
If t.Tag
is null, the cast to a value type throws a NullReferenceException
. 如果
t.Tag
为null,则t.Tag
转换为值类型将引发NullReferenceException
。 Check that you have indeed initialized Tag
for all instances of UCSAlloc
检查您是否确实为
UCSAlloc
所有实例初始化了Tag
当强制转换失败时(无论出于何种原因),“ crtl”本身为null,因为如果强制转换失败,则使用“ as”进行强制转换将返回null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.