Windows Develop Bookmark and Share   
 index > Windows Forms General > Help dealing with nulls
 

Help dealing with nulls

I was interested in the proper way to deal with null objects. 
I have a Contact Class that contains an Email Collection class.
In certain circumstances the email collection will contain no emails.
When i do something like:
lvContact.SubItems.Add(contact.Emails[0].EmailAddress);
This will throw an aurgument out of range exception since there is no objects in the collection.

I changed the email collection to look like this:

public class EmailCollection: CollectionBase
{
public Email this[int index] 
{
get 
{
if ( List.Count != 0 )
{
return (Email)List[index];
}
else
{
return new Email(0,string.Empty,true);
}
}
set {List[index] = value;}

......

This works but is this the RIGHT way to do this?

MigrationUser 1  Tuesday, April 05, 2005 9:14 AM
Well, the right thing to do would be to check if the object is Null (or Nothing) before trying to access the collection.

If Not IsNothing(Contact.Emails) Then 'get email address

I'm not 100% sure of the C# equivelent,  but I would guess it is something like:

Not Contact.Emails Is Null

And actually, you should just be able to check if Contact.Emails.Count > 0...
MigrationUser 1  Tuesday, April 05, 2005 10:44 AM
So its better to do something like this than dealing with it in the class:

if (contact.Emails.Count > 0) {
lvContact.SubItems.Add(contact.Emails[0].EmailAddress);
} else {
lvContact.SubItems.Add("");
}



MigrationUser 1  Tuesday, April 05, 2005 12:21 PM
brohar,

The best thing to do is not only validate the count, but the value itself.

if (contact.Emails.Count > 0 &&
    contact.Emails[0].EmailAddress != null &&
    contact.Emails[0].EmailAddress.Length > 0) {

    lvContact.SubItems.Add(contact.Emails[0].EmailAddress);
}


You don't want a list full of nulls.  It's unnecessary, and empty strings are just as bad in my opinion.
MigrationUser 1  Tuesday, April 05, 2005 12:26 PM
Yes, in my opinion it is better to handle it directly rather than indirectly through the class.  This way the collection still behaves the way a collection is expected to behave.
MigrationUser 1  Tuesday, April 05, 2005 12:30 PM

The best way is to simply check the number of items in the collection before accessing them by looking at the "Count" property of your collection.

MigrationUser 1  Thursday, April 07, 2005 10:54 AM
I'd instantiate the Email Collection regardless of if there are any emails or not, in the Contact constructor.  That way the client code won't fail because the Email Collection will always exist even if its count is 0.   Then you can use the foreach statement

foreach (Email in Contacts.Email)
{
//Do stuff and check for possible null field values
}

MigrationUser 1  Thursday, April 07, 2005 7:43 PM

You can use google to search for other answers

Custom Search

More Threads

• TextAlign in TextBox triggers Leave Event
• Painting problem
• Custom control drawing problem
• toggling ToolStripMenuItem state
• MDI Control Box Behavior
• How do I add table functionality to a richTextBox control?
• WindowsMediaPlayerClass: modifying mediaCollection
• printing multiple textboxes from the form
• Where do I store the dataset?
• Modifying the SplitContainer's Splitter Preview Beam