forwardFIX Message   Table of ContentFIX Sessionforward
Manipulating Message Fields

A FIX message represents a sequence of fields whose values are associated with unique numbers (tags). Such an interpretation treats a message as a collection of values associated with tag numbers. For this reason, the primary approach in handling message fields is similar to managing associative collections.

Adding Field into Message

To associate a field with a value, the Set(Int32, String) method must be used. If there is no field of a given tag number available in the message, this member creates an association. If the field already exists, this member updates its value with the new value.

Note Note

It is also possible to get or set fields values using Message’s indexer.

Also, there are Set methods for each standard type, e.g. Set(Int32, Int32). Such methods transform input value into text-presentation, so the actually-stored value is a text-presentation of input. Therefore, when the message is sent there is no need to serialize such values. If you do not perform a large amount of typed set/get operations with the message object on the critical path, then these methods are the best choice from the performance point of view. In addition, there are similar SetV methods, e.g. SetV(Int32, Int32). Such methods set values as it is without transforming it into text-presentation. Assigning field value via SetV methods saves not only value and its type as well, therefore no parsing is performed during subsequent access to the field via typed Get methods. In this case, when the message is sent there is need to serialize such values, however, if you perform a large amount of typed set/get operations with the message object on the critical path, then these methods are the best choice from the performance point of view.

Short String Optimization

Message class supports the Short String Optimization (SSO). If the size of the string presentation of a field value less than the size of the internal field value buffer (15 bytes for 32-bit and 23 bytes for 64-bit) then such a value is stored directly in this buffer without any new memory allocation. Therefore, setting short string values is faster because the dynamical memory allocation is not performed. Also, subsequent access to a field with the short string can work faster due to the better data locality, however, please note that SSO is not applied if a long string is set earlier to the field.
Accessing Field Value

To get a field value, the Get(Int32) method must be used.

Note Note

It is also possible to get or set fields values using Message’s indexer.

Also, there are typed Get. and TryGet. methods, e.g GetInteger(Int32), TryGetInteger(Int32, Int32). Such methods perform the conversion of stored value to the corresponding type. Typed Get. methods throw an exception if the conversion fails and typed TryGet. methods return false in such cases.

Checking Field Presence

To check the field presence, the Contain(Int32) method must be used.

Removing Field from Message

To remove a field, the Remove(Int32) method must be used.

Example

The following example demonstrates the basic operations over message fields.

C#
string ClOrdID = order[11];
execReport[17] = "Unique identifier of execution message";
order.Remove(57);
VB
Dim ClOrdID As String = order.Get(11)
execReport.Set(17, "Unique identifier of execution message")
order.Remove(57)
GC-free interface

The following interface does not allocate temporary string objects and gives an ability to manipulate fields without a GC pressure:

Such the interface also exists for Group and FlatMessage classes.

Please see the "Using GC-free interface" article of the Low Latency Best Practices page for details.

Enumerating Message Fields

Message class supports the IEnumerable interface, therefore you can use a Message object in the foreach operator to enumerate fields of the message. Additionally, the Fields property exposed by the Message class to obtain the collection of all fields currently available in the message. When you enumerate fields of a message, this does not include fields from a repeating group. To enumerate fields of the repeating group you need to get the corresponding GroupInstance object from the repeating group, please see the FIX Repeating Groups.

Example

The following example dumps all message fields onto the system console.

C#
using FIXForge.NET.FIX;
using FIXForge.NET.FIX.FIX44;

Message msg = new Message("D", ProtocolVersion.FIX44);

foreach(Field field in msg)
  Console.WriteLine("Tags={0}, value={1}", field.Tag, field.Value);
VB
Imports  FIXForge.NET.FIX
Imports  FIXForge.NET.FIX.FIX44

Dim msg As New Message("D", ProtocolVersion.FIX44)

Dim field As Field
For Each field In msg
  Console.WriteLine("Tags={0}, value={1}", field.Tag, field.Value)
Next field
Named Constants

In each sub-namespace, which corresponds to certain FIX version (like FIXForge.NET.FIX.FIX44), there is a Tags class defined. This class contains the constants for all-known tag numbers. Additionally, there are classes, which contains the constants for all-known tag values. The usage of all of these constants makes the source code more readable.

Example
C#
using FIXForge.NET.FIX;
using FIXForge.NET.FIX.FIX44;

Message order = new Message(
  MsgType.Order_Single, ProtocolVersion.FIX44);

order.Set(Tags.ClOrdID, "90001008");
order.Set(Tags.Side, Side.Buy);
order.Set(Tags.TimeInForce, TimeInForce.Day);
VB
Imports FIXForge.NET.FIX
Imports FIXForge.NET.FIX.FIX44

Dim order As New Message(
  MsgType.Order_Single, ProtocolVersion.FIX44)

order.Set(Tags.ClOrdID, "90001008")
order.Set(Tags.Side, Side.Buy)
order.Set(Tags.TimeInForce, TimeInForce.Day)

See Also