Click here to Skip to main content
15,891,864 members
Articles / Web Development / ASP.NET

Unraveling the Mysteries of .NET 2.0 Configuration

Rate me:
Please Sign up or sign in to vote.
4.83/5 (352 votes)
28 Jun 2007CPOL35 min read 2M   4.9K   916  
Learn to utilize powerful new .NET 2.0 configuration features
In this article, you will learn how to utilize the powerful new .NET 2.0 configuration features to simplify and centralize your configuration code.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

namespace Examples
{
	public class ArticlesElementCollection: ConfigurationElementCollection
	{
		#region Constructor
		public ArticlesElementCollection()
		{
		}
		#endregion

		#region Properties
		public override ConfigurationElementCollectionType CollectionType
		{
			get
			{
				return ConfigurationElementCollectionType.BasicMap;
			}
		}
		protected override string ElementName
		{
			get
			{
				return "article";
			}
		}

		protected override ConfigurationPropertyCollection Properties
		{
			get
			{
				return new ConfigurationPropertyCollection();
			}
		}
		#endregion

		#region Indexers
		public ArticleElement this[int index]
		{
			get
			{
				return (ArticleElement)base.BaseGet(index);
			}
			set
			{
				if (base.BaseGet(index) != null)
				{
					base.BaseRemoveAt(index);
				}
				base.BaseAdd(index, value);
			}
		}

		public ArticleElement this[string name]
		{
			get
			{
				return (ArticleElement)base.BaseGet(name);
			}
		}
		#endregion

		#region Methods
		public void Add(ArticleElement item)
		{
			base.BaseAdd(item);
		}

		public void Remove(ArticleElement item)
		{
			base.BaseRemove(item);
		}

		public void RemoveAt(int index)
		{
			base.BaseRemoveAt(index);
		}
		#endregion

		#region Overrides
		protected override ConfigurationElement CreateNewElement()
		{
			return new ArticleElement();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			return (element as ArticleElement).Name;
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Jon Rista has been programming since the age of 8 (first Pascal program), and has been a programmer since the age of 10 (first practical program). In the last 21 years, he has learned to love C++, embrace object orientation, and finally enjoy the freedom of C#. He knows over 10 programming languages, and vows that his most important skill in programming is creativity, even more so than logic. Jon works on large-scale enterprise systems design and implementation, and employs Design Patterns, C#, .NET, and SQL Server in his daily doings.

Comments and Discussions