HELP with IList please

D

Darren

I'm trying to bound a custom object to a combobox using DataSource
property. The code works correctly however when the user selects a
different value I get a runtime error of NullReferenceException -
Object reference not set to an instance of an object. I've posted a
snippet of the code below: I think it maybe something to do with
CopyTo or SyncRoot as I don't understand what these are. Any please
will be appreciated.

Thanks,
Darren.



myCombo.DataSource = new ItemDescCollection();




public struct ItemDesc
{
private string sName;
private int nVal;

public ItemDesc(string pName, int pVal)
{
this.sName = pName;
this.nVal = pVal;
}
public string Name
{
get {return sName;}
}
public int Value
{
get {return nVal;}
}
public override string ToString()
{
return sName;
}
}

public class ItemDescCollection : IList
{
ArrayList nItems = new ArrayList();
public ItemDescCollection()
{

// Initialize the collection with two items
ItemDesc newItem = new ItemDesc("First", 23 );
nItems.Add(newItem);
newItem = new ItemDesc("Second", 25 );
nItems.Add(newItem);
}

// ICollection
public int Count { get {return nItems.Count;} }
public bool IsSynchronized {get {return false; }}
public object SyncRoot { get { return nItems.SyncRoot; }}
public void CopyTo (System.Array array, int index ){return;}

// IEnumerable
public IEnumerator GetEnumerator(){return nItems.GetEnumerator();}

// IList
public bool IsFixedSize {get {return false;}}
public bool IsReadOnly {get {return false;}}
public int Add( object value ){return nItems.Add(value);}
public void Clear(){nItems.Clear();}
public bool Contains( object value ){return nItems.Contains(value);}
public int IndexOf( object value ){return nItems.IndexOf( value );}
public void Insert ( int index, object value ){nItems.Insert(index,
value);}
public void Remove( object value ){nItems.Remove( value );}
public void RemoveAt( int index ){nItems.RemoveAt( index );}
public object this[int index]
{
get { return (ItemDesc)nItems[index]; }
set { nItems[index] = value; }
}
}
 
A

Alexander Shirshov

Darren,

ComboBox wants you to copy items into its internal storage. You should implement
CopyTo:

public void CopyTo (System.Array array, int index ){
for (int i = index; i < Count; i++)
{
array.SetValue(nItems, i);
}
}


HTH,
Alexander Shirshov
 
J

Jon Skeet [C# MVP]

Darren said:
I'm trying to bound a custom object to a combobox using DataSource
property. The code works correctly however when the user selects a
different value I get a runtime error of NullReferenceException -
Object reference not set to an instance of an object. I've posted a
snippet of the code below: I think it maybe something to do with
CopyTo or SyncRoot as I don't understand what these are. Any please
will be appreciated.

Well SyncRoot looks okay, but unlike all the rest of the methods, you
haven't proxied CopyTo at all - you should call
nItems.CopyTo(array, index).

By the way, were you aware that because all your items are in an
ArrayList, they're being boxed and unboxed all the time? Do you have
any particular reason for making ItemDesc a struct rather than a class?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top