473,385 Members | 1,353 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,385 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 1916
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 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...
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...
0
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...
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...
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...
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); }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.