HOME
ABOUT ME
Biography
How I Became Boog
The Real Boog Powell
My Resume
WRITING
Stories
Reviews For Writers
Writing Links
CONTACT
   
   

Visit my other site:
Bleeker Books

Friends Of Boog


Recent Posts


Complete Archives

"Ah, but I was so much older then, I'm younger than that now."

My Boog Pages


Wednesday, December 13
The Season of Giving

Just thought I'd pass along a link to World Vision's Gift Catalog (via FARK). Here you can donate various goods to impoverished areas in Africa, and it's as easy as ordering from Amazon.com.

I gave a share of a well, because clean water is one of the biggest problems in the developing world. It sounds silly, but many children in the developing world die from chronic diahrrea, usually caused by contaminated drinking water.

I also gave a share of a bull. Like there's not enough bullshit in the world.

Tags: .


posted by Graham at 1:32 PM permalink

Trackbacks | Comments (0)

The .NET Dictionary Serialization problem, solved by KeyedCollection

When I posted a while back about importing XML documents as objects using serialization, one of the purposes I wanted to put this to was creating a list of objects that could be selected by a unique key value. For example, if you had a list of books, you could pick out the one you wanted by specifiying its ISBN.

In .NET, this kind of lookup is handled by a type of collection called a dictionary. You give it a key and a value, and Presto! You can sort it, look up specific items, etc.

One problem: dictionaries don't support serialization.

I spent a while banging my head against this brick wall before I found a convenient way around it: a .NET class called KeyedCollection. KeyedCollection derives from the iList interface instead of iDictionary, and is therefore serializable, but also allows you to specify a key. This class is an abstract type, so you must derive your own custom class, but as we'll see in a second, that's a snap.

KeyedCollection must be inherited because it has to be a list of a specific object type. Then, instead of specifying your own key value for each item in the list, you indicate which of the object's fields you want to be used as the key. Here is a class I created this morning:

Public Class SourceTypeList
  Inherits System.Collections.ObjectModel.KeyedCollection(Of Long, SourceType)

    Protected Overrides Function GetKeyForItem(ByVal item As SourceType) As Long
      Return item.SourceTypeID
    End Function

    Sub New()
      MyBase.New()
    End Sub
End Class


What does this code do? It tells Visual Basic to create a new type collection derived from KeyedCollection, where the key is a long integer and the value is an object of type SourceType (SourceType represents information about a type of syndication file, such as Atom 0.3 or RSS 2.0). You then overried the function GetKeyForItem and tell VB which field you want to use as the key.

And it works beautifully. I had tested deserialization using a generic List(Of T) and I was able to swap out the code in maybe 5 minutes.

So if you need a keyed list and your objects include unique values, you can use KeyedCollection and get the benefits of serialization as well.

Tags: , , .


posted by Graham at 12:51 PM permalink

Trackbacks | Comments (0)


This page is powered by Blogger. Isn't yours?

Listed on BlogShares