Thursday, February 09, 2012
 
 View Article   
08

 

 

Let me start by saying, I am a strong believer in strong typing. From my point of view, typless collections (for example) have nothing to do with object oriented programming! One of my basic programming philosophies is: let the compiler catch as many errors as possible. Having this attitude, the generics in C# 2.0, is for me a major milestone.

Due to this, and on earlier experience with other programming languages, I was really looking forward C# 2.0 with generics.

My first task, was simply to migrate some collections I had in the Log4Net.Dashboard. In this application I had written all data collections based on two class, one class which was the value class en one class which was the collection class.

For example in my configuration handler, I had two classes responsible for storing the configuration information about possible datasources in the config file. I will firs show you how I did this before generics was available.

The value class (DataSourceItem) looked something like:

public class DataSourceItem

{

private string mName;

private string mConnectionString;

private string mL4NTableName;

// ++++ get/set properties for the members

// …

}

And then I usually implemented a collection class based on the CollectionBase class, in this case the DataSourceItemCollection:

public class DataSourceItemCollection : CollectionBase

{

///

/// Search the collection for a DataSourceItem

///

/// Search parameter, the name to find

/// The DataSourceItem object width the matching name or null if name is not found

public DataSourceItem FindByName(string SearchName)

{

foreach (DataSourceItem li in List)

{

if (String.Compare(li.Name, SearchName) == 0)

return li;

}

return null; // not found

}

///

/// Gets og sets DataSourceItem based on index

///

public DataSourceItem this[int index]

{

get

{

return ((DataSourceItem)List[index]);

}

set

{

List[index] = value;

}

}

///

/// Adds a new DataSourceIteme element to the collection

///

///

///

public int Add(DataSourceItem value)

{

return (List.Add(value));

}

///

/// Finds the index of a DataSource Item

///

/// The Object to search for

/// The Index

public int IndexOf(DataSourceItem value)

{

return (List.IndexOf(value));

}

///

/// Insers a DataSourceItem in the collection

///

/// The index i.e. where in the list should the item be inserted

/// The DataSourceItem

public void Insert(int index, DataSourceItem value)

{

List.Insert(index, value);

}

public void Remove(DataSourceItem value)

{

List.Remove(value);

}

public bool Contains(DataSourceItem value)

{

return (List.Contains(value));

}

protected override void OnInsert(int index, Object value)

{

// Insert additional code to be run only when inserting values.

}

protected override void OnRemove(int index, Object value)

{

// Insert additional code to be run only when removing values.

}

protected override void OnSet(int index, Object oldValue, Object newValue)

{

// Insert additional code to be run only when setting values.

}

protected override void OnValidate(Object value)

{

if (value.GetType() != Type.GetType("L4NDash.Config.DataSourceItem"))

throw new ArgumentException("value must be of type L4NDash.Config.DataSourceItem.", value.ToString());

}

}

Having used this approach, I was well prepared to take advantage of the generics in C# 2.0, rewrote the DataSourceItemCollection, and inherited from the generic collection class, System.Collections.ObjectModel.Collection:

public class DataSourceItemCollection : System.Collections.ObjectModel.Collection<DataSourceItem>

{

///

/// Search the collection for a DataSourceItem

///

/// Search parameter, the name to find

/// The DataSourceItem object width the matching name or null if name is not found

public DataSourceItem FindByName(string SearchName)

{

foreach (DataSourceItem li in this)

{

if (String.Compare(li.Name, SearchName) == 0)

return li;

}

return null; // not found

}

}

As you can se from the code, a lot of source code is wiped out, making my application easier to maintain.

Well, generics has a lot more to it, this was just the first encounter. But at least I now longer need to write collections based on the CollectionBase class.

 

Posted in: Programming Tips

Post Rating

Comments

sam
# sam
Wednesday, January 27, 2010 6:47 AM
great

Post Comment

Only registered users may post comments.
Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement