Search code examples
c#combobox

Why my combobox is null? even though not beeing null


I am using c1 combobox from another class, but value is null but actually not. Default value is "8". It is working on Form1 class properly I can see values that I picked, but Form2

Form1.cs

c1.Items.Add(8);

Form1.Designer.cs

 public static System.Windows.Forms.ComboBox c1;

Form2.cs class

 MessageBox.Show(Form1.c1.Text);

Solution

  • The code to set the value is actually executed in Form1's context only. Since you made it a static field without understanding what you are doing that initialization code does not get a chance to execute and code in Form2 happily sees the null value.

    So says my psychic debugging about this ill-defined problem statement.