473,802 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 NullReferenceEx ception -
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.DataSou rce = new ItemDescCollect ion();


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 ItemDescCollect ion : IList
{
ArrayList nItems = new ArrayList();
public ItemDescCollect ion()
{

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

// 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.GetEnume rator();}

// IList
public bool IsFixedSize {get {return false;}}
public bool IsReadOnly {get {return false;}}
public int Add( object value ){return nItems.Add(valu e);}
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.Remove At( index );}
public object this[int index]
{
get { return (ItemDesc)nItem s[index]; }
set { nItems[index] = value; }
}
}
Jul 21 '05 #1
2 1935
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********@hot mail.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 NullReferenceEx ception -
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(a rray, 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.co m>
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
2849
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 IList. Now, I'm trying to bind object to a combo box. The bidning happens correctly, but I'm unable to set the DisplayMember property of the combo box (the count of items in the combo box gives me the correct value, but no items are displayed in the...
5
3543
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 obtained via the Enumerator that I create in the GetEnumerator() method. I have tried elplicitly implementing IEnumerable on my sublass as well. Can anyone help on this? Thanks in advance.
3
2204
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 fine till I have to create a Stack object - all sorts of error messages are generated. I have included source files below: The List class: #include "List.h" template <class T>
0
955
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 - 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.
2
297
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 - 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.
1
5634
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, but I can't seem to figure the the get enumerator section. I try: //IEnumerable public IEnumerator GetEnumerator()
2
4535
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 ReadOnlyCollection<T> : IList<T>,ICollection<T>,IEnumerable<T> ...
4
12848
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 the List<T> type. Could some one tell me where can I find the IList implementation? Please note that the plase where I am looking for this implementation is from the Visula Studio metadata information page. The page that shows up when you...
8
2219
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
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.