-1

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.

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;
        }
    }
}
Omer Keidar
  • 82
  • 1
  • 10

2 Answers2

2

The condition in your Where is wrong:

.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; 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. Instead you should use the Equals method:

.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; in this case it will call the Int32.Equals method, which will correctly return true if the tag is 123.


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. Check that you have indeed initialized Tag for all instances of UCSAlloc

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
0

"crtl" itself is null when the cast fails (for whatever reason) because casting with "as" returns null if the cast fails.

Paul Weiland
  • 727
  • 10
  • 24
  • true, but the casting doesn't fail here. i checked the item after the cast and it was ok. The Tag property, along with some other properties, is null – Omer Keidar Oct 20 '14 at 09:17