1. Welcome Guest! In order to create a new topic or reply to an existing one, you must register first. It is easy and free. Click here to sign up now!.
    Dismiss Notice

What's wrong with my configuration code?

Discussion in 'Microsoft News' started by NewsBot, Apr 3, 2008.

  1. NewsBot

    NewsBot I Got News

    Joined:
    Feb 8, 2006
    Messages:
    1,813
    Operating System:
    Windows Vista Home Premium
    I'm not a fan of the new .NET 2.0 Configuration model, I find it's too long-winded and takes away the simplicity of .NET 1.x's ISectionHandler approach.

    ...that, plus the reflection makes me shudder.

    Anyway, here's my app.config:
















    And here's my Configuration code

    public sealed class ManageThisSection : ConfigurationSection {

    public ManageThisSection() {

    }

    [ConfigurationProperty("competitions")]
    [ConfigurationCollection(typeof(ConfigurationElementCollection))]
    public CompetitionsElement Competitions {
    get { return (CompetitionsElement)base["competitions"]; }
    }

    public sealed class CompetitionsElement : ConfigurationElement {

    private CompetitionElementCollection _competitions;

    public CompetitionsElement() {
    _competitions = new CompetitionElementCollection();
    }

    [ConfigurationProperty("", IsDefaultCollection=true)]
    public CompetitionElementCollection Competitions {
    get { return _competitions; }
    }

    public CompetitionElement GetCompetitionInfo(Int32 id) {
    foreach(CompetitionElement ele in _competitions) {
    if(ele.Id == id) return ele;
    }
    return null;
    }

    }

    public sealed class CompetitionElementCollection : ConfigurationElementCollection {

    public override ConfigurationElementCollectionType CollectionType {
    get {
    return ConfigurationElementCollectionType.AddRemoveClearMap;
    }
    }

    protected override ConfigurationElement CreateNewElement() {
    return new CompetitionElement();
    }

    protected override Object GetElementKey(ConfigurationElement element) {
    return (element as CompetitionElement).Id;
    }

    public CompetitionElement this[Int32 index] {
    get { return (CompetitionElement)BaseGet(index); }
    set {
    if( BaseGet(index) != null ) {
    BaseRemoveAt(index);
    }
    BaseAdd(index, value);
    }
    }

    public void Add(CompetitionElement element) {
    base.BaseAdd( element );
    }

    public void Clear() {
    base.BaseClear();
    }

    public void Remove(CompetitionElement element) {
    base.BaseRemove(element.Id);
    }

    public void Remove(Int32 id) {
    base.BaseRemove(id);
    }

    public void RemoveAt(Int32 index) {
    base.BaseRemoveAt(index);
    }
    }

    public sealed class CompetitionElement : ConfigurationElement {

    [ConfigurationProperty("id",IsKey=true,IsRequired=true)]
    public Int32 Id {
    get { return (Int32)base["id"]; }
    set { base["id"] = value; }
    }

    [ConfigurationProperty("name",IsRequired=true)]
    public String Name {
    get { return (String)base["name"]; }
    set { base["name"] = value; }
    }

    [ConfigurationProperty("path",IsRequired=true)]
    public String Path {
    get { return (String)base["path"]; }
    set { base["path"] = value; }
    }

    }

    }
    ...why isn't it working?




     

Share This Page