PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Spss/SpssStringVariable.cs

#
C# | 152 lines | 89 code | 20 blank | 43 comment | 11 complexity | 3af31c00c374f31859727a4ebaf5606c MD5 | raw file
Possible License(s): LGPL-2.1
  1. //-----------------------------------------------------------------------
  2. // <copyright file="SpssStringVariable.cs" company="Andrew Arnott">
  3. // Copyright (c) Andrew Arnott. All rights reserved.
  4. // Copyright (c) Brigham Young University
  5. // </copyright>
  6. //-----------------------------------------------------------------------
  7. namespace Spss {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. /// <summary>
  12. /// Represents an SPSS data variable that stores character string information.
  13. /// </summary>
  14. public class SpssStringVariable : SpssVariable {
  15. private readonly SpssStringVariableValueLabelsDictionary valueLabels;
  16. private int length = -1;
  17. /// <summary>
  18. /// Creates an instance of the <see cref="SpssStringVariable"/> class,
  19. /// for use when defining a new variable.
  20. /// </summary>
  21. public SpssStringVariable() {
  22. this.valueLabels = new SpssStringVariableValueLabelsDictionary(this);
  23. this.MissingValues = new List<string>(3);
  24. }
  25. /// <summary>
  26. /// Creates an instance of the <see cref="SpssStringVariable"/> class,
  27. /// for use in loading variables from an existing SPSS data file.
  28. /// </summary>
  29. /// <param name="variables">The containing collection.</param>
  30. /// <param name="varName">The name of the variable being loaded.</param>
  31. /// <param name="length">The length of the string variable. This is the same as SpssType</param>
  32. protected internal SpssStringVariable(SpssVariablesCollection variables, string varName, int length)
  33. : base(variables, varName) {
  34. this.valueLabels = new SpssStringVariableValueLabelsDictionary(this);
  35. this.length = length;
  36. MissingValueFormatCode formatCode;
  37. string[] missingValues = new string[3];
  38. ReturnCode result = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarCMissingValues(this.FileHandle, this.Name, out formatCode, out missingValues[0], out missingValues[1], out missingValues[2]), "spssGetVarCMissingValues", ReturnCode.SPSS_SHORTSTR_EXP);
  39. if (result == ReturnCode.SPSS_OK) {
  40. this.MissingValues = new List<string>(missingValues.Take((int)formatCode));
  41. } else {
  42. this.MissingValues = new List<string>(0);
  43. }
  44. }
  45. /// <summary>
  46. /// Gets the maximum length a string in this variable can be.
  47. /// </summary>
  48. public int Length {
  49. get {
  50. return length >= 0 ? length : ColumnWidth;
  51. }
  52. set {
  53. VerifyNotCommittedVariable();
  54. if (value < 0) throw new ArgumentOutOfRangeException("Length", value, "Must be a non-negative number.");
  55. length = value;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets or sets the missing values for this variable.
  60. /// </summary>
  61. /// <value>The missing values.</value>
  62. /// <remarks>
  63. /// A maximum of three maximum values may be supplied.
  64. /// </remarks>
  65. public IList<string> MissingValues { get; private set; }
  66. /// <summary>
  67. /// Gets the SPSS type for the variable.
  68. /// </summary>
  69. public override int SpssType {
  70. get {
  71. return Length;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets or sets the data value of this variable within a specific case.
  76. /// </summary>
  77. internal new string Value {
  78. get {
  79. string v;
  80. SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetValueChar(FileHandle, Handle, out v), "spssGetValueChar");
  81. return v;
  82. }
  83. set {
  84. if (value == null) value = string.Empty;
  85. if (value.Length > Length)
  86. throw new ArgumentOutOfRangeException("Value", value, "String too long for variable " + Name + ". Maximum length is: " + Length);
  87. SpssSafeWrapper.spssSetValueChar(FileHandle, Handle, value);
  88. }
  89. }
  90. /// <summary>
  91. /// The set of value labels (response values and labels) that are defined.
  92. /// </summary>
  93. public IDictionary<string, string> ValueLabels {
  94. get { return this.valueLabels; }
  95. }
  96. /// <summary>
  97. /// Updates the changed attributes of the variable within SPSS.
  98. /// </summary>
  99. protected override void Update() {
  100. base.Update();
  101. if (!IsInCollection) return; // we'll get to do this later
  102. this.valueLabels.Update();
  103. string[] missingValues = new string[3];
  104. this.MissingValues.Take(missingValues.Length).ToArray().CopyTo(missingValues, 0);
  105. // We allow failure due to long string var types only if we're not actually setting any missing values.
  106. ReturnCode[] allowedReturnCodes = this.MissingValues.Count > 0 ? new ReturnCode[0] : new ReturnCode[] { ReturnCode.SPSS_SHORTSTR_EXP };
  107. SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarCMissingValues(
  108. this.FileHandle,
  109. this.Name,
  110. (MissingValueFormatCode)Math.Min(this.MissingValues.Count, missingValues.Length),
  111. missingValues[0],
  112. missingValues[1],
  113. missingValues[2]), "spssSetVarCMissingValues", allowedReturnCodes);
  114. }
  115. public override SpssVariable Clone() {
  116. SpssStringVariable other = new SpssStringVariable();
  117. CloneTo(other);
  118. return other;
  119. }
  120. protected override void CloneTo(SpssVariable spssVar) {
  121. base.CloneTo(spssVar);
  122. SpssStringVariable other = spssVar as SpssStringVariable;
  123. if (other == null)
  124. throw new ArgumentException("Must be of type " + GetType().Name + ".", "other");
  125. other.Length = Length;
  126. this.valueLabels.CopyTo(other.valueLabels);
  127. }
  128. protected override bool IsApplicableFormatTypeCode(FormatTypeCode formatType) {
  129. return formatType == FormatTypeCode.SPSS_FMT_A;
  130. }
  131. }
  132. }