DataError.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DataError.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. // <owner current="false" primary="false">[....]</owner>
  8. //------------------------------------------------------------------------------
  9. namespace System.Data {
  10. using System;
  11. using System.Diagnostics;
  12. /// <devdoc>
  13. /// <para>Represents an custom error that can be associated with a <see cref='System.Data.DataRow'/>.</para>
  14. /// </devdoc>
  15. internal sealed class DataError {
  16. private string rowError = String.Empty;
  17. // column-level errors
  18. private int count;
  19. private ColumnError[] errorList;
  20. internal const int initialCapacity = 1;
  21. internal DataError() {
  22. }
  23. internal DataError(string rowError) {
  24. SetText(rowError);
  25. }
  26. internal string Text {
  27. get {
  28. return rowError;
  29. }
  30. set {
  31. SetText(value);
  32. }
  33. }
  34. internal bool HasErrors {
  35. get {
  36. return(rowError.Length != 0 || count != 0);
  37. }
  38. }
  39. //
  40. // this method resets the error to the new value.
  41. //
  42. internal void SetColumnError(DataColumn column, string error) {
  43. Debug.Assert(column != null, "Invalid (null) argument");
  44. Debug.Assert(column.Table != null, "Invalid (loose) column");
  45. if (error == null || error.Length == 0) {
  46. // remove error from the collection
  47. Clear(column);
  48. }
  49. else {
  50. if (errorList == null) {
  51. errorList = new ColumnError[initialCapacity];
  52. }
  53. int i = IndexOf(column);
  54. errorList[i].column = column;
  55. errorList[i].error = error;
  56. column.errors++;
  57. if (i == count)
  58. count++;
  59. }
  60. }
  61. internal string GetColumnError(DataColumn column) {
  62. for (int i = 0; i < count; i++) {
  63. if (errorList[i].column == column) {
  64. return errorList[i].error;
  65. }
  66. }
  67. return String.Empty;
  68. }
  69. internal void Clear(DataColumn column) {
  70. if (count == 0)
  71. return;
  72. for (int i = 0; i < count; i++) {
  73. if (errorList[i].column == column) {
  74. System.Array.Copy(errorList, i+1, errorList, i, count-i-1);
  75. count--;
  76. column.errors--;
  77. Debug.Assert(column.errors >= 0, "missing error counts");
  78. }
  79. }
  80. }
  81. internal void Clear() {
  82. for (int i = 0; i < count; i++) {
  83. errorList[i].column.errors--;
  84. Debug.Assert(errorList[i].column.errors >= 0, "missing error counts");
  85. }
  86. count = 0;
  87. rowError = String.Empty;
  88. }
  89. internal DataColumn[] GetColumnsInError() {
  90. DataColumn[] cols = new DataColumn[count];
  91. for (int i = 0; i < count; i++) {
  92. cols[i] = errorList[i].column;
  93. }
  94. return cols;
  95. }
  96. /// <devdoc>
  97. /// <para>Sets the error message for the <see cref='System.Data.DataError'/>.</para>
  98. /// </devdoc>
  99. private void SetText(string errorText) {
  100. if (null == errorText) {
  101. errorText = String.Empty;
  102. }
  103. rowError = errorText;
  104. }
  105. internal int IndexOf (DataColumn column) {
  106. // try to find the column
  107. for (int i = 0; i < count; i++) {
  108. if (errorList[i].column == column) {
  109. return i;
  110. }
  111. }
  112. if (count >= errorList.Length) {
  113. int newCapacity = Math.Min(count*2, column.Table.Columns.Count);
  114. ColumnError[] biggerList = new ColumnError[newCapacity];
  115. System.Array.Copy(errorList, 0, biggerList, 0, count);
  116. errorList = biggerList;
  117. }
  118. return count;
  119. }
  120. internal struct ColumnError {
  121. internal DataColumn column;
  122. internal string error;
  123. };
  124. }
  125. }