PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Spss/SpssStringVariableValueLabels.cs

#
C# | 87 lines | 47 code | 8 blank | 32 comment | 6 complexity | 7d28d4b9b142184e511748829a029c62 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections;
  4. namespace Spss
  5. {
  6. /// <summary>
  7. /// A collection of value labels for a <see cref="SpssStringVariable"/>.
  8. /// </summary>
  9. public class SpssStringVariableValueLabelsDictionary : SpssVariableValueLabelsDictionary<string>
  10. {
  11. /// <summary>
  12. /// Creates an instance of the <see cref="SpssStringVariableValueLabelsDictionary"/> class.
  13. /// </summary>
  14. public SpssStringVariableValueLabelsDictionary(SpssVariable variable)
  15. : base(variable, StringComparer.Ordinal)
  16. {
  17. }
  18. #region Attributes
  19. /// <summary>
  20. /// The variable hosting this collection.
  21. /// </summary>
  22. protected new SpssStringVariable Variable { get { return (SpssStringVariable) base.Variable; } }
  23. /// <summary>
  24. /// Gets a value indicating whether this string variable can have value labels.
  25. /// </summary>
  26. /// <remarks>
  27. /// SPSS only supports value labels on strings within
  28. /// <see cref="SpssThinWrapper.SPSS_MAX_SHORTSTRING"/> characters
  29. /// in length..
  30. /// </remarks>
  31. public bool Applies
  32. {
  33. get
  34. {
  35. return Variable.Length <= SpssSafeWrapper.SPSS_MAX_SHORTSTRING;
  36. }
  37. }
  38. #endregion
  39. #region Operations
  40. /// <summary>
  41. /// Adds a value label.
  42. /// </summary>
  43. /// <param name="value">
  44. /// The response value to associate with the new response label.
  45. /// </param>
  46. /// <param name="label">
  47. /// The new response label.
  48. /// </param>
  49. public override void Add(string value, string label)
  50. {
  51. if( !Applies ) throw new InvalidOperationException("Cannot add value labels to a long string variable.");
  52. base.Add(value, label);
  53. }
  54. /// <summary>
  55. /// Updates the SPSS data file with changes made to the collection.
  56. /// </summary>
  57. protected internal override void Update()
  58. {
  59. foreach( var pair in this )
  60. SpssSafeWrapper.spssSetVarCValueLabel(FileHandle, Variable.Name, pair.Key, pair.Value);
  61. }
  62. /// <summary>
  63. /// Initializes the value labels dictionary from the SPSS data file.
  64. /// </summary>
  65. protected override void LoadFromSpssFile() {
  66. if (!Applies) return;
  67. string[] values;
  68. string[] labels;
  69. ReturnCode result = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarCValueLabels(FileHandle, Variable.Name, out values, out labels), "spssGetVarCValueLabels", ReturnCode.SPSS_NO_LABELS);
  70. if (result == ReturnCode.SPSS_OK) { // ReturnCode.SPSS_NO_LABELS is nothing special -- just no labels to add
  71. Debug.Assert(values.Length == labels.Length);
  72. for (int i = 0; i < values.Length; i++) {
  73. Add(values[i], labels[i]);
  74. }
  75. }
  76. }
  77. #endregion
  78. }
  79. }