471,338 Members | 1,031 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

HELP with IList please

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; }
}
}
Jul 21 '05 #1
2 1821
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], i);
}
}
HTH,
Alexander Shirshov
Jul 21 '05 #2
Darren <wa********@hotmail.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Darren | last post: by
2 posts views Thread by Darren | last post: by
1 post views Thread by Paul | last post: by
2 posts views Thread by Lucian Wischik | last post: by
4 posts views Thread by Rene | last post: by
8 posts views Thread by julia_beresford | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.