473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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; }
}
}
Nov 16 '05 #1
0 938

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: CGuy | last post by:
URGENT HELP REQUIRED FROM GURUS Hi, I have a custom object that implements ICollection and IListSource. This object has also an enumerator defined for it which implements IEnumerator and...
5
by: Steve M | last post by:
I have subclassed CollectionBase. I have also implemented GetEnumerator(). I have tried to set the DataSource of a DataGrid to an instance of my subclass. However, the items in the grid are not...
2
by: Darren | last post by:
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...
3
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
1
by: Paul | last post by:
Hi all, I'm trying to implement IList and keep getting an error when trying to implement GetEnumerator(). My class has a List<String> and I've been using its methods as return types for IList,...
2
by: Lucian Wischik | last post by:
Does ReadOnlyCollection<T> really implement IList<T>, like it claims to do? ... When I right-click on ReadOnlyCollection and look at its definition, it says this: public class...
4
by: Rene | last post by:
According to the documentation, the List<T> type explicitly implements the non generic IList interface. The problem is that no matter how hard I look, I am not able to find this implemetion on...
6
by: nicolas.rolland | last post by:
Would anyone know the reson why IList<Tdoes not implements IList ?? This results in strange behaviours, like typeof(IList).IsAssignableFrom(typeof(List<string>)) --true...
8
by: julia_beresford | last post by:
Hi I need to create the following class at runtime: public class MyCollection : CollectionBase { public void Add(MyItem item) { List.Add(item); }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.