PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Spss/SpssStringVariableValueLabels.cs

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