Pair

From Xojo Documentation

Class (inherits from Object)

Stores key-value pairs.

Properties
Left Right
Constructors

Constructor(left as Variant, right as Variant)


Notes

Use the : operator to assign the Left and Right values of a single Pair in a Var statement. For example:

Var p As Pair = 1 : 2

This statement assigns the "1" to the Left property and the "2" to the Right property.

The following is also acceptable:

Var p As New Pair(1, 2)

The values of the Left and Right properties cannot be assigned using the = operator. For example, the code:

Var p As Pair = 1 : 2
p.Left = 23

produces a Cannot Assign a Value to this Property error.

Sample Code

This example creates a Pair instance and assigns its Left and Right properties to a String:

Var p As Pair = 5 : 6
Var values As String = p.Left.StringValue + ", " + p.Right.StringValue

To create a linked list:

Var p As Pair = 1 : 2 : 3 : 4

The Left property of the first pair will contain the "1" and the right property will contain a second pair. The second pair's Left property will contain the "2" and its Right property will contain the third pair, and so forth. This statement creates four linked pairs.

This example uses an array of Pairs to obtain a class instance's properties and values using the Introspection system:

Var tcp As New TCPSocket
Var ti As Introspection.TypeInfo = Introspection.GetType(tcp)
Var props() As Pair
For Each prop As Introspection.PropertyInfo In ti.GetProperties
props.Add(prop.Name : prop.Value(tcp))
Next prop

The following code displays the Name/Value pairs that were obtained in the previous example in a two-column ListBox:

For Each p As Pair In props
Try
Listbox1.Addrow(p.Left.StringValue)
Listbox1.CellValueAt(Listbox1.LastAddedRowIndex, 1) = p.Right.StringValue
Catch err As TypeMismatchException
// ignore the problem
End Try
Next

See Also

Dictionary class; : operator.