Gs1Element Class

Represents a GS1 element string: an application identifier (AI) and the data it qualifies, such as AI 01 (GTIN) and 09521234543213.

public sealed class Gs1Element

Remarks

The data does not include the parentheses of the human-readable interpretation: they are printed under the bars but are not encoded. By default, the application identifier and the data are checked against the syntax rules every GS1 element follows. Use Gs1ValidationMode.Dictionary to also check them against the GS1 dictionary of application identifiers (the format of the data of each identifier, its check digits, dates, ...), and Gs1Element.ValidateAssociations to check the identifiers that must or must not be used together.

Constructors

NameDescription
Gs1Element(string, string)Initializes a new instance of the Gs1Element class, checked against the syntax rules of element strings.
Gs1Element(string, string, Gs1ValidationMode)Initializes a new instance of the Gs1Element class.

Gs1Element overloads

Gs1Element(string, string)

Gs1Element.cs

Initializes a new instance of the Gs1Element class, checked against the syntax rules of element strings.

public Gs1Element(string applicationIdentifier, string value)
Parameters
applicationIdentifier string
The application identifier: 2 to 4 digits, without parentheses.
value string
The data: characters of the GS1 AI encodable character set 82 (letters, digits and !"%&'()*+,-./:;<=>?_), and # for the application identifier 8010. The application identifiers whose data has a predefined length (00 to 04, 11 to 20, 31nn to 36nn and 41n) require digits of that length.
Exceptions
ArgumentException
The application identifier or the data is not valid.

Gs1Element(string, string, Gs1ValidationMode)

Gs1Element.cs

Initializes a new instance of the Gs1Element class.

public Gs1Element(string applicationIdentifier, string value, Gs1ValidationMode validationMode)
Parameters
applicationIdentifier string
The application identifier: 2 to 4 digits, without parentheses.
value string
The data that the application identifier qualifies.
validationMode Gs1ValidationMode
The rules the application identifier and the data are checked against.
Exceptions
ArgumentException
The application identifier or the data is not valid, or, with Gs1ValidationMode.Dictionary, the application identifier is not in the GS1 dictionary or the data does not match its format.
Example
// The syntax rules accept any data of 14 digits for a GTIN
var element = new Gs1Element("01", "09521234543214");
Assert.Equal("(01)09521234543214", element.ToString());

// The GS1 dictionary also checks the check digit, the dates, the country codes, ...
var exception = Assert.Throws<ArgumentException>("value", () => new Gs1Element("01", "09521234543214", Gs1ValidationMode.Dictionary));
Assert.Contains("The numeric check digit is incorrect.", exception.Message);

var gtin = new Gs1Element("01", "09521234543213", Gs1ValidationMode.Dictionary);
var expiry = new Gs1Element("17", "250100", Gs1ValidationMode.Dictionary); // day 00: the end of the month
var barcode = Barcode.CreateGs1128(gtin, expiry);
Assert.Equal("(01)09521234543213(17)250100", barcode.Text);

Properties

NameDescription
ApplicationIdentifierGets the application identifier, such as 01.
ValueGets the data that the application identifier qualifies.

ApplicationIdentifier

Gs1Element.cs

Gets the application identifier, such as 01.

public string ApplicationIdentifier { get; }

Property value

string

Gets the data that the application identifier qualifies.

public string Value { get; }

Property value

string

Methods

NameDescription
ToString()Returns the human-readable interpretation of the element string, such as (01)09521234543213.
ValidateAssociations(ReadOnlySpan<Gs1Element>)Checks the associations of application identifiers that the GS1 dictionary defines: the identifiers that an identifier requires, such as a batch number (10) that requires a GTIN (01), and the identifiers that cannot be used together, such as a GTIN (01) and the GTIN of the contained items (02). An application identifier cannot appear twice with different data.

ToString()

Gs1Element.cs

Returns the human-readable interpretation of the element string, such as (01)09521234543213.

public override string ToString()

Returns

string The application identifier in parentheses, followed by the data.

ValidateAssociations(ReadOnlySpan<Gs1Element>)

Gs1Element.cs

Checks the associations of application identifiers that the GS1 dictionary defines: the identifiers that an identifier requires, such as a batch number (10) that requires a GTIN (01), and the identifiers that cannot be used together, such as a GTIN (01) and the GTIN of the contained items (02). An application identifier cannot appear twice with different data.

public static void ValidateAssociations(params scoped ReadOnlySpan<Gs1Element> elements)

Parameters

elements ReadOnlySpan<Gs1Element>
The elements marked on an item. Pass the elements of every barcode of the item: an identifier may require an identifier encoded in another barcode. The application identifiers that are not in the dictionary have no rules.

Exceptions

ArgumentException
elements contains null, a required application identifier is missing, two application identifiers cannot be used together, or an application identifier appears twice with different data.

Example

var gtin = new Gs1Element("01", "09521234543213", Gs1ValidationMode.Dictionary);
var batch = new Gs1Element("10", "ABC123", Gs1ValidationMode.Dictionary);
Gs1Element.ValidateAssociations(gtin, batch);

// A batch number qualifies a trade item: it requires a GTIN
Assert.Throws<ArgumentException>("elements", () => Gs1Element.ValidateAssociations(batch));