internal static SpssVariable LoadVariable(SpssVariablesCollection parent, string varName, int varType) { FormatTypeCode writeFormat, printFormat; int writeDecimal, writeWidth, printDecimal, printWidth; SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarWriteFormat(parent.Document.Handle, varName, out writeFormat, out writeDecimal, out writeWidth), "spssGetVarWriteFormat"); SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarPrintFormat(parent.Document.Handle, varName, out printFormat, out printDecimal, out printWidth), "spssGetVarPrintFormat"); SpssVariable variable; switch (varType) { case 0: // This may be a date or a numeric if (SpssDateVariable.IsDateVariable(writeFormat)) variable = new SpssDateVariable(parent, varName, writeFormat, writeWidth, printFormat, printWidth); else variable = new SpssNumericVariable(parent, varName, writeFormat, writeDecimal, writeWidth, printFormat, printDecimal, printWidth); break; default: Debug.Assert(varType == printWidth); variable = new SpssStringVariable(parent, varName, varType); break; } return variable; }
/// <summary> /// Creates an instance of the <see cref="SpssNumericVariable"/> class. /// </summary> /// <param name="variables">The containing collection.</param> /// <param name="varName">The name of the variable.</param> protected SpssVariable(SpssVariablesCollection variables, string varName) { if (variables == null) throw new ArgumentNullException("variables"); if (varName == null || varName.Length == 0) throw new ArgumentNullException("varName"); this.variables = variables; AssumeIdentity(varName); }
/// <summary> /// Initializes a new instance of the <see cref="SpssDateVariable"/> class. /// for use in loading variables from an existing SPSS data file. /// </summary> /// <param name="variables">The containing collection.</param> /// <param name="varName">The name of the variable.</param> /// <param name="writeFormat">The write format.</param> /// <param name="writeWidth">Width of the write.</param> /// <param name="printFormat">The print format.</param> /// <param name="printWidth">Width of the print.</param> protected internal SpssDateVariable(SpssVariablesCollection variables, string varName, FormatTypeCode writeFormat, int writeWidth, FormatTypeCode printFormat, int printWidth) : base(variables, varName) { MissingValueFormatCode formatCode; double[] missingValues = new double[3]; ReturnCode result = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarNMissingValues(this.FileHandle, this.Name, out formatCode, out missingValues[0], out missingValues[1], out missingValues[2]), "spssGetVarNMissingValues"); this.MissingValueFormat = formatCode; this.MissingValues = new List<DateTime>(missingValues.Take(Math.Abs((int)formatCode)).Select(v => ConvertDoubleToDateTime(v))); this.writeFormat = writeFormat; this.writeWidth = writeWidth; this.printFormat = printFormat; this.printWidth = printWidth; }
/// <summary> /// Creates an instance of the <see cref="SpssNumericVariable"/> class, /// for use in loading variables from an existing SPSS data file. /// </summary> /// <param name="variables">The containing collection.</param> /// <param name="varName">The name of the variable.</param> /// <param name="writeFormat">The write format. The default is <see cref="FormatTypeCode.SPSS_FMT_F"/></param> /// <param name="writeDecimal">The write decimal.</param> /// <param name="writeWidth">Width of the write.</param> /// <param name="printFormat">The print format. The default is <see cref="FormatTypeCode.SPSS_FMT_F"/></param> /// <param name="printDecimal">The print decimal.</param> /// <param name="printWidth">Width of the print.</param> protected internal SpssNumericVariable(SpssVariablesCollection variables, string varName, FormatTypeCode writeFormat, int writeDecimal, int writeWidth, FormatTypeCode printFormat, int printDecimal, int printWidth) : base(variables, varName) { this.valueLabels = new SpssNumericVariableValueLabelsDictionary(this); MissingValueFormatCode formatCode; double[] missingValues = new double[3]; ReturnCode result = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarNMissingValues(this.FileHandle, this.Name, out formatCode, out missingValues[0], out missingValues[1], out missingValues[2]), "spssGetVarNMissingValues"); this.MissingValueFormat = formatCode; this.MissingValues = new List<double>(missingValues.Take(Math.Abs((int)formatCode))); this.writeDecimal = writeDecimal; this.writeWidth = writeWidth; this.writeFormat = writeFormat; this.printDecimal = printDecimal; this.printWidth = printWidth; this.printFormat = printFormat; }
/// <summary> /// Creates an instance of the <see cref="SpssStringVariable"/> class, /// for use in loading variables from an existing SPSS data file. /// </summary> /// <param name="variables">The containing collection.</param> /// <param name="varName">The name of the variable being loaded.</param> /// <param name="length">The length of the string variable. This is the same as SpssType</param> protected internal SpssStringVariable(SpssVariablesCollection variables, string varName, int length) : base(variables, varName) { this.valueLabels = new SpssStringVariableValueLabelsDictionary(this); this.length = length; MissingValueFormatCode formatCode; string[] missingValues = new string[3]; 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); if (result == ReturnCode.SPSS_OK) { this.MissingValues = new List<string>(missingValues.Take((int)formatCode)); } else { this.MissingValues = new List<string>(0); } }
/// <summary> /// Informs this variable that it is being removed from a <see cref="SpssVariablesCollection"/>. /// </summary> internal void RemoveFromCollection(SpssVariablesCollection variables) { if (variables == null) throw new ArgumentNullException("variables"); if (variables != Variables) throw new ArgumentException("The variables collection being removed from does not match the collection this variable belongs to."); Variables.Document.DictionaryCommitted -= new EventHandler(Document_DictionaryCommitted); this.variables = null; // remove reference to owning collection }
/// <summary> /// Informs this variable that it is being added to a <see cref="SpssVariablesCollection"/>. /// </summary> /// <exception cref="InvalidOperationException"> /// Thrown when this variable already belongs to a different collection. /// </exception> internal void AddToCollection(SpssVariablesCollection variables) { if (variables == null) throw new ArgumentNullException("variables"); if (Variables != null && Variables != variables) throw new InvalidOperationException("Already belongs to a different collection."); if (Name == null || Name.Length == 0) throw new InvalidOperationException("SpssVariable.Name must be set first."); // Make sure that a variable with this same name has not already been added to the collection. if (variables.Contains(Name) && !variables.Contains(this)) // and not this one throw new SpssVariableNameConflictException(Name); this.variables = variables; Variables.Document.DictionaryCommitted += new EventHandler(Document_DictionaryCommitted); }