2002-04-28 Rodrigo Moya <rodrigo@ximian.com>
[mono.git] / mcs / class / System.Data / System.Data / DataTableCollection.cs
1 //
2 // System.Data.DataTableCollection.cs
3 //
4 // Author:
5 //   Christopher Podurgiel (cpodurgiel@msn.com)
6 //
7 // (C) Chris Podurgiel
8 //
9
10 using System;
11
12 namespace System.Data
13 {
14         /// <summary>
15         /// Represents the collection of tables for the DataSet.
16         /// </summary>
17         public class DataTableCollection : InternalDataCollectionBase
18         {
19                 private DataTable[] tables = null;
20                 private int size = 0;
21
22                 public virtual DataTable Add () {
23                         DataTable table = new DataTable ();
24                         DataTable[] tmp = new DataTable[size + 1];
25
26                         if (size > 0)
27                                 Array.Copy (tables, tmp, size);
28                         size++;
29                         tables = tmp;
30                         tables[size - 1] = table;
31
32                         return table;
33                 }
34
35                 public virtual void Add (DataTable table) {
36                         DataTable[] tmp = new DataTable[size + 1];
37
38                         Array.Copy (tables, tmp, size);
39                         size++;
40                         tables = tmp;
41                         tables[size - 1] = table;
42                 }
43
44                 public virtual DataTable Add (string name) {
45                         DataTable table = this.Add ();
46                         table.TableName = name;
47
48                         return table;
49                 }
50
51                 public void AddRange (DataTable[] tables_to_add) {
52                         for (int i = 0; i < tables_to_add.Length; i++) {
53                                 this.Add (tables_to_add[i]);
54                         }
55                 }
56
57                 [MonoTODO]
58                 [Serializable]
59                 public bool CanRemove (DataTable table) {
60                         throw new NotImplementedException ();
61                 }
62
63                 public void Clear () {
64                         /* FIXME: is this correct? */
65                         tables = null;
66                         size = 0;
67                 }
68
69                 public bool Contains (string name) {
70                         for (int i = 0; i < size; i++) {
71                                 if (tables[i].TableName == name)
72                                         return true;
73                         }
74
75                         return false;
76                 }
77
78                 public void CopyTo (Array ar, int index) {
79                         Array.Copy (tables, ar, size);
80                 }
81
82                 public virtual int IndexOf (DataTable table) {
83                         for (int i = 0; i < size; i++) {
84                                 if (tables[i] == table)
85                                         return i;
86                         }
87
88                         return -1;
89                 }
90
91                 public virtual int IndexOf (string name) {
92                         for (int i = 0; i < size; i++) {
93                                 if (tables[i].TableName == name)
94                                         return i;
95                         }
96
97                         return -1;
98                 }
99
100                 public void Remove (DataTable table) {
101                         this.RemoveAt (this.IndexOf (table));
102                 }
103
104                 public void Remove (string name) {
105                         this.RemoveAt (this.IndexOf (name));
106                 }
107
108                 [MonoTODO]
109                 public void RemoveAt (int index) {
110                         throw new NotImplementedException ();
111                 }
112                 
113                 public override int Count {
114                         get { return size; }
115                 }
116
117                 // IsReadOnly and IsSynchronized must be implemented or
118                 // is it safe to use InternalDataCollectionBase's
119
120                 public DataTable this[int index] {
121                         get {
122                                 if (index < size)
123                                         return tables[index];
124                                 return null;
125                         }
126                 }
127
128                 public DataTable this[string name] {
129                         get {
130                                 for (int i = 0; i < size; i++) {
131                                         if (tables[i].TableName == name)
132                                                 return tables[i];
133                                 }
134
135                                 return null;
136                         }
137                 }
138
139                 [MonoTODO]
140                 protected override ArrayList List {
141                         throw new NotImplementedException ();
142                 }
143                 
144                 [Serializable]
145                 public event CollectionChangeEventHandler CollectionChanged;
146
147                 [Serializable]
148                 public event CollectionChangeEventHandler CollectionChanging;
149
150                 [Serializable]
151                 protected virtual void OnCollectionChanged (
152                         CollectionChangeEventArgs ccevent) {
153                 }
154
155                 [Serializable]
156                 protected internal virtual void OnCollectionChanging (
157                         CollectionChangeEventArgs ccevent) {
158                 }
159         }
160 }