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; }
}
}
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; }
}
}