/// <summary> /// Sets the specified value in the specified field, creating an IsoValue internally. /// </summary> /// <param name="index">The field number (2 to 128)</param> /// <param name="value">The value to be stored.</param> /// <param name="encoder">An optional CustomField to encode/decode the value.</param> /// <param name="t"></param> /// <param name="length"></param> /// <returns></returns> public IsoMessage SetValue(int index, object value, ICustomField encoder, IsoType t, int length) { if (index < 2 || index > 128) { throw new IndexOutOfRangeException("Field index must be between 2 and 128"); } if (value == null) { _fields[index] = null; } else { IsoValue v; v = t.NeedsLength() ? new IsoValue(t, value, length, encoder) : new IsoValue(t, value, encoder); v.Encoding = Encoding; _fields[index] = v; } return(this); }
public CompositeField AddValue(object val, ICustomField encoder, IsoType t, int length) { return(AddValue(t.NeedsLength() ? new IsoValue(t, val, length, encoder) : new IsoValue(t, val, encoder))); }
public IsoValue(IsoType t, object val, int len, ICustomField custom = null) { Type = t; Value = val; Length = len; Encoder = custom; if (Length == 0 && t.NeedsLength()) { throw new ArgumentException($"Length must be greater than zero for type {t} (value '{val}')"); } switch (t) { case IsoType.LLVAR: case IsoType.LLLVAR: case IsoType.LLLLVAR: if (len == 0) { Length = Encoder?.EncodeField(val).Length ?? val.ToString().Length; } if (t == IsoType.LLVAR && Length > 99) { throw new ArgumentException("LLVAR can only hold values up to 99 chars"); } if (t == IsoType.LLLVAR && Length > 999) { throw new ArgumentException("LLLVAR can only hold values up to 999 chars"); } if (t == IsoType.LLLLVAR && Length > 9999) { throw new ArgumentException("LLLLVAR can only hold values up to 9999 chars"); } break; case IsoType.LLBIN: case IsoType.LLLBIN: case IsoType.LLLLBIN: if (len == 0) { if (Encoder == null) { var obj = val; Length = ((byte[])obj).Length; } else if (Encoder is ICustomBinaryField) { var customBinaryField = (ICustomBinaryField)custom; if (customBinaryField != null) { Length = customBinaryField.EncodeBinaryField(Value).Length; } } else { Length = Encoder.EncodeField(Value).Length; } Length = Encoder?.EncodeField(Value).Length ?? ((sbyte[])val).Length; } if (t == IsoType.LLBIN && Length > 99) { throw new ArgumentException("LLBIN can only hold values up to 99 chars"); } if (t == IsoType.LLLBIN && Length > 999) { throw new ArgumentException("LLLBIN can only hold values up to 999 chars"); } if (t == IsoType.LLLLBIN && Length > 9999) { throw new ArgumentException("LLLLBIN can only hold values up to 9999 chars"); } break; } }
public IsoValue(IsoType t, object value, ICustomField custom = null) { if (t.NeedsLength()) { throw new ArgumentException("Fixed-value types must use constructor that specifies length"); } Encoder = custom; Type = t; Value = value; switch (Type) { case IsoType.LLVAR: case IsoType.LLLVAR: case IsoType.LLLLVAR: if (Encoder == null) { Length = value.ToString().Length; } else { var enc = Encoder.EncodeField(value) ?? (value?.ToString() ?? string.Empty); Length = enc.Length; } if (t == IsoType.LLVAR && Length > 99) { throw new ArgumentException("LLVAR can only hold values up to 99 chars"); } if (t == IsoType.LLLVAR && Length > 999) { throw new ArgumentException("LLLVAR can only hold values up to 999 chars"); } if (t == IsoType.LLLLVAR && Length > 9999) { throw new ArgumentException("LLLLVAR can only hold values up to 9999 chars"); } break; case IsoType.LLBIN: case IsoType.LLLBIN: case IsoType.LLLLBIN: if (Encoder == null) { if (value.GetType() == typeof(sbyte[])) { var obj = value; Length = ((sbyte[])obj).Length; } else { Length = value.ToString().Length / 2 + value.ToString().Length % 2; } } else if (Encoder is ICustomBinaryField) { Length = ((ICustomBinaryField)Encoder).EncodeBinaryField(value).Length; } else { var enc = Encoder.EncodeField(value) ?? (value?.ToString() ?? string.Empty); Length = enc.Length; } if (t == IsoType.LLBIN && Length > 99) { throw new ArgumentException("LLBIN can only hold values up to 99 chars"); } if (t == IsoType.LLLBIN && Length > 999) { throw new ArgumentException("LLLBIN can only hold values up to 999 chars"); } if (t == IsoType.LLLLBIN && Length > 9999) { throw new ArgumentException("LLLLBIN can only hold values up to 9999 chars"); } break; default: Length = Type.Length(); break; } }