1e26511964760df58ea0287a4dc61c25568d0126
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConnectionStringSettingsCollection.cs
1 //
2 // System.Configuration.ConnectionStringSettingsCollection.cs
3 //
4 // Authors:
5 //   Sureshkumar T <tsureshkumar@novell.com>
6 //   Chris Toshok <toshok@ximian.com>
7 //
8 //
9 // Copyright (C) 2004,2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32
33 using System;
34
35 namespace System.Configuration
36 {
37         [ConfigurationCollection (typeof (ConnectionStringSettings),
38                                   CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
39         public sealed class ConnectionStringSettingsCollection : ConfigurationElementCollection
40         {
41
42                 public ConnectionStringSettingsCollection () : base ()
43                 {
44                 }
45
46                 public new ConnectionStringSettings this [string Name]
47                 {
48                         get {
49                                 foreach (ConfigurationElement c in this) {
50                                         if (!(c is ConnectionStringSettings))
51                                                 continue;
52                                         if (string.Compare(((ConnectionStringSettings) c).Name, Name, true, 
53                                                 System.Globalization.CultureInfo.InvariantCulture) == 0)
54                                                 return c as ConnectionStringSettings;
55
56                                 }
57                                 return null;
58                         }
59                 }
60
61                 public new ConnectionStringSettings this [int index]
62                 {
63                         get { return (ConnectionStringSettings) BaseGet (index); }
64                         set {
65                                 if (BaseGet (index) != null)
66                                         BaseRemoveAt (index);
67                                 BaseAdd (index, value);
68                         }
69                 }
70
71                 [MonoTODO]
72                 protected internal override ConfigurationPropertyCollection Properties {
73                         get { return base.Properties; }
74                 }
75
76
77                 protected override ConfigurationElement CreateNewElement ()
78                 {
79                         return new ConnectionStringSettings ();
80                 }
81
82                 protected override object GetElementKey (ConfigurationElement element)
83                 {
84                         return ((ConnectionStringSettings) element).Name;
85                 }
86
87                 public void Add (ConnectionStringSettings settings)
88                 {
89                         BaseAdd ((ConfigurationElement) settings);
90                 }
91
92                 public void Clear ()
93                 {
94                         BaseClear ();
95                 }
96
97                 public int IndexOf (ConnectionStringSettings settings)
98                 {
99                         return BaseIndexOf (settings);
100                 }
101
102                 public void Remove (ConnectionStringSettings settings)
103                 {
104                         BaseRemove (settings.Name);
105                 }
106
107                 public void Remove (string name)
108                 {
109                         BaseRemove (name);
110                 }
111
112                 public void RemoveAt (int index)
113                 {
114                         BaseRemoveAt (index);
115                 }
116
117                 protected override void BaseAdd (int index, ConfigurationElement element)
118                 {
119                         if (!(element is ConnectionStringSettings))
120                                 base.BaseAdd (element);
121                         if (IndexOf ((ConnectionStringSettings) element) >= 0)
122                                 throw new ConfigurationException (String.Format ("The element {0} already exist!",
123                                                                                  ((ConnectionStringSettings) element).Name));
124                         this [index] = (ConnectionStringSettings) element;
125                 }
126         }
127
128 }
129 #endif // NET_2_0