ParameterCollection.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using FastReport.Utils;
  6. namespace FastReport.Data
  7. {
  8. /// <summary>
  9. /// Represents the collection of <see cref="Parameter"/> objects.
  10. /// </summary>
  11. public class ParameterCollection : FRCollectionBase
  12. {
  13. /// <summary>
  14. /// Gets or sets a parameter.
  15. /// </summary>
  16. /// <param name="index">The index of a parameter in this collection.</param>
  17. /// <returns>The parameter with specified index.</returns>
  18. public Parameter this[int index]
  19. {
  20. get { return List[index] as Parameter; }
  21. set { List[index] = value; }
  22. }
  23. /// <summary>
  24. /// Finds a parameter by its name.
  25. /// </summary>
  26. /// <param name="name">The name of a parameter.</param>
  27. /// <returns>The <see cref="Parameter"/> object if found; otherwise <b>null</b>.</returns>
  28. public Parameter FindByName(string name)
  29. {
  30. foreach (Parameter c in this)
  31. {
  32. // check complete match or match without case sensitivity
  33. if (c.Name == name || c.Name.ToLower() == name.ToLower())
  34. return c;
  35. }
  36. return null;
  37. }
  38. /// <summary>
  39. /// Returns an unique parameter name based on given name.
  40. /// </summary>
  41. /// <param name="name">The base name.</param>
  42. /// <returns>The unique name.</returns>
  43. public string CreateUniqueName(string name)
  44. {
  45. string baseName = name;
  46. int i = 1;
  47. while (FindByName(name) != null)
  48. {
  49. name = baseName + i.ToString();
  50. i++;
  51. }
  52. return name;
  53. }
  54. /// <summary>
  55. /// Copies the parameters from other collection.
  56. /// </summary>
  57. /// <param name="source">Parameters to copy from.</param>
  58. public void Assign(ParameterCollection source)
  59. {
  60. Clear();
  61. foreach (Parameter par in source)
  62. {
  63. Parameter thisParam = new Parameter(par.Name);
  64. Add(thisParam);
  65. thisParam.DataType = par.DataType;
  66. thisParam.Value = par.Value;
  67. thisParam.Expression = par.Expression;
  68. thisParam.Description = par.Description;
  69. thisParam.Parameters.Assign(par.Parameters);
  70. }
  71. }
  72. private void EnumParameters(ParameterCollection root, SortedList<string, Parameter> list)
  73. {
  74. foreach (Parameter p in root)
  75. {
  76. if (!list.ContainsKey(p.FullName))
  77. {
  78. list.Add(p.FullName, p);
  79. EnumParameters(p.Parameters, list);
  80. }
  81. }
  82. }
  83. internal void AssignValues(ParameterCollection source)
  84. {
  85. SortedList<string, Parameter> this_list = new SortedList<string, Parameter>();
  86. EnumParameters(this, this_list);
  87. SortedList<string, Parameter> source_list = new SortedList<string, Parameter>();
  88. EnumParameters(source, source_list);
  89. foreach (KeyValuePair<string, Parameter> kv in source_list)
  90. {
  91. if (this_list.ContainsKey(kv.Key))
  92. this_list[kv.Key].Value = kv.Value.Value;
  93. }
  94. }
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="ParameterCollection"/> class with default settings.
  97. /// </summary>
  98. /// <param name="owner">The owner of this collection.</param>
  99. public ParameterCollection(Base owner) : base(owner)
  100. {
  101. }
  102. }
  103. }