0

Why this code works

// in a method
var query = a query;
    comboBox.DataSource = new BindingSource(query, null);   
    comboBox.DisplayMember = "Value";           
    comboBox.ValueMember = "Key";

but the code below not

// in a class
public object DataSource
{
    set { comboBox.DataSource = value; }
}

// in a method
var query = a query;
    aclass.DataSource = new BindingSource(query, null);   
    aclass.DisplayMember = "Value";           
    aclass.ValueMember = "Key";

the error: "Object reference not set to an instance of an object."

This code does not work, too:

BindingSource bs = new BindingSource(query, null);
aclass.DataSource = bs;
Mohsen
  • 323
  • 1
  • 6
  • 15
  • You'll have to learn to debug your code so you can find the nullreference. Probably in this case the combox is null in the second example. – Carra Apr 14 '17 at 07:18
  • @carra query is not null. The problem should be in instantiating BindingSource. – Mohsen Apr 14 '17 at 07:24

0 Answers0