Пример #1
0
        //============================================================================
        /// <summary>
        /// The new member to add to this structure.
        /// </summary>
        /// <param name="typedValue">Typed value to copy.</param>
        // N.Herrmann Apr 2002
        //============================================================================
        protected void initTypeCopy(TTypedValue typedValue)
        {
            uint i;

            Name = typedValue.Name;
            FBaseType = typedValue.baseType();
            FIsScalar = typedValue.isScalar();
            FIsArray = typedValue.isArray();
            FIsRecord = typedValue.isRecord();
            setUnits(typedValue.units());

            if (FIsScalar)
            {
                createScalar();
                switch (FBaseType)
                {                                           //For scalars, copy the value data.
                    case TBaseType.ITYPE_INT1:                                            //Data pertaining to arrays and records
                    case TBaseType.ITYPE_INT2:                                            //   is ultimately stored in their
                    case TBaseType.ITYPE_INT4:                                            //   constituent scalars
                    case TBaseType.ITYPE_INT8: setValue(typedValue.asInt()); break;
                    case TBaseType.ITYPE_SINGLE: setValue(typedValue.asSingle()); break;
                    case TBaseType.ITYPE_DOUBLE: setValue(typedValue.asDouble()); break;
                    case TBaseType.ITYPE_BOOL: setValue(typedValue.asBool()); break;
                    case TBaseType.ITYPE_CHAR:
                    case TBaseType.ITYPE_WCHAR: setValue(typedValue.asChar()); break;
                    case TBaseType.ITYPE_STR:
                    case TBaseType.ITYPE_WSTR: setValue(typedValue.asStr()); break;
                }
            }
            else if (FIsArray || FIsRecord)
            {
                uint iCount = typedValue.count();
                if (FIsArray && (iCount == 0))
                {
                    if (typedValue.item(0) != null)
                        newMember(typedValue.item(0));
                    setElementCount(0);
                }
                else
                    for (i = 1; i <= iCount; i++)
                        newMember(typedValue.item(i)); //clones and adds this typed value
            }
        }
Пример #2
0
        //======================================================================
        /// <summary>
        /// Assignment from a TTypedValue that need not be of identical type, but must   
        /// be type-compatible.
        /// When converting from a scalar string to a numeric an exception will be thrown
        /// if the source string is not a valid numeric.
        /// </summary>
        /// <param name="srcValue">The source typed value.</param>
        /// <returns>True is the value can be set.</returns>
        //======================================================================
        public Boolean setValue(TTypedValue srcValue)
        {
            bool result = false;
            bool bCompatible;

            if (srcValue != null)
            {
                bCompatible = ((FIsScalar == srcValue.isScalar()) && (FIsArray == srcValue.isArray()));
                if (FBaseType == TBaseType.ITYPE_DEF)
                {
                    bCompatible = (bCompatible && (srcValue.baseType() == TBaseType.ITYPE_DEF));
                }
                if (!bCompatible)
                {
                    String error = String.Format("Incompatible assignment from {0} to {1}\nCannot convert {2} to {3}", Name, srcValue.Name, srcValue.baseType().ToString(), FBaseType.ToString());
                    throw (new TypeMisMatchException(error));
                }
                if (FIsScalar)
                {
                    try
                    {
                        switch (FBaseType)
                        {
                            case TBaseType.ITYPE_INT1:
                            case TBaseType.ITYPE_INT2:
                            case TBaseType.ITYPE_INT4:
                                {
                                    result = setValue(srcValue.asInt());
                                    break;
                                }
                            case TBaseType.ITYPE_INT8:
                                result = setValue(Convert.ToInt64(srcValue.asDouble()));
                                break;
                            case TBaseType.ITYPE_DOUBLE:
                                {
                                    result = setValue(srcValue.asDouble());
                                    break;
                                }
                            case TBaseType.ITYPE_SINGLE:
                                {
                                    result = setValue(srcValue.asSingle());
                                    break;
                                }
                            case TBaseType.ITYPE_BOOL:
                                {
                                    result = setValue(srcValue.asBool());
                                    break;
                                }
                            default:
                                {
                                    result = setValue(srcValue.asStr());
                                    break;
                                }
                        }
                    }
                    catch
                    {
                        throw (new Exception("setValue() cannot convert " + srcValue.asStr() + " to " + FBaseType.ToString()));
                    }
                }
                else
                {
                    if (FIsArray)
                    {
                        setElementCount(srcValue.count());
                    }
                    uint iCount = count();
                    for (uint Idx = 1; Idx <= iCount; Idx++)
                    {
                        result = item(Idx).setValue(srcValue.item(Idx));
                    }
                }
            }
            return result;
        }