Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / September 2005

Tip: Looking for answers? Try searching our database.

Custom collection design/style opinion requested

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Dunaway - 30 Sep 2005 20:08 GMT
VB.Net:  I have create a custom collection to hold instances of my
custom class.  The collection has an Item property which is set as the
default property.

I can use code such as this to retrieve an item by index or key:

Dim MyInst As MyCustomClass = MyColl("key")

My question is how should the situation of the key (or index) not
present in the list be handled?

Right now, if "key" was not in the collection, the collection would
throw an exception.  To deal with that I would have to code like this:

Dim MyInst As MyCustomClass
Try
  MyInst = MyColl("key")

  'Process instance here

Catch ex As KeyNotFoundException
  'Do something here like display a message, or whatever
End Try

But I am wondering if it would be better to not throw an exception but
to simply return Nothing if the key is not found.  Then the code would
be like this:

Dim MyInst As MyCustomClass
MyInst = MyColl("key")
If MyInst Is Nothing Then
  'Log error, display message, or whatever
Else
  'Process instance here
End If

The cost of the exception in the first method would be high the first
time, but not afterwards, whereas the second method is more defensive.
Or would it be better to do something like this:

If MyColl.ContainsKey("key") Then
  Dim MyInst As MyCustomClass = MyColl("key")
  'Process instance here
Else
  'Display message that key not found here
End If

What, if any, is the "standard" or best practice for such things?

Thanks for any opinions.

Chris
Keith Patrick - 30 Sep 2005 21:55 GMT
Exceptions incur significant overhead (especially with regards to your call
stack), so if your collection cannot contain null items, I'd say return
null, but if you do allow keying of null slots (i.e. myList["myKey"] =
null;), you will need to go the exception route to differentiate between the
two scenarios (ItemNotFound vs. ItemFoundButIsNull)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.