Platforms to show: All Mac Windows Linux Cross-Platform

Back to AtomicDictionaryMBS class.

AtomicDictionaryMBS.Clear

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Removes all entries from the Dictionary.

Since API 2, Xojo prefers RemoveAll instead of Clear.

AtomicDictionaryMBS.CompareAndSet(key As Variant, expectedValue as Variant, newValue as Variant) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Compare a value and set it.
Example
Var a As New AtomicDictionaryMBS
a.Value(1) = 2

Var r1 As Boolean = a.CompareAndSet(1, 2, 3) // works
Var r2 As Boolean = a.CompareAndSet(1, 2, 5) // value doesn't match
Var r3 As Boolean = a.CompareAndSet(1, 3, 4) // works

a.Value("test") = "Hello"

Var t1 As Boolean = a.CompareAndSet("test", "abc", "World") // value doesn't match
Var t2 As Boolean = a.CompareAndSet("test", "Hello", "found") // works

Break // inspect in debugger

If the value for key matches the expectedValue parameter, this functions set the entry to the new value and returns true. Otherwise it returns false.

This allows you to do a comparison and then exchange if the value matches.
Currently we only compare strings, numbers and booleans by value and others by reference.

AtomicDictionaryMBS.Constructor(Clone as AtomicDictionaryMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a copy of the atomic dictionary.
Example
Var a As New AtomicDictionaryMBS

a.Value("Hello") = "World"
a.Value("test") = 2

Var c As New AtomicDictionaryMBS(a)
MessageBox c.Value("Hello")

See also:

AtomicDictionaryMBS.Constructor(values as Dictionary = nil)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a new dictionary.
Example
Var a As New Dictionary

a.Value("Hello") = "World"
a.Value("test") = 2

Var c As New AtomicDictionaryMBS(a)
MessageBox c.Value("Hello")

Optionally pass in an existing dictionary to copy the content.

See also:

AtomicDictionaryMBS.ExecuteLocked(theDelegate as DictionaryActionMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Runs the delegate while the dictionary is locked.
Example
Sub UpdateDic(a as AtomicDictionaryMBS)
a.Value("Hello") = a.Value("Hello") + 1
End Sub

Sub Test
Var a As New AtomicDictionaryMBS

a.Value("Hello") = 1

a.ExecuteLocked AddressOf UpdateDic

Var v As Variant = a.Value("Hello")

Break
End Sub

AtomicDictionaryMBS.GetAndSet(key As Variant, newValue as Variant, defaultValue as Variant = nil) as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Gets a value and sets a new value.
Example
Var a As New AtomicDictionaryMBS

a.Value(1) = 1

Var r1 As Boolean = a.GetAndSet(1, 2) // gets 1, sets 2
Var r2 As Boolean = a.GetAndSet(2, 3) // sets 3

MessageBox a.Value(1)+" "+a.Value(2) // 2 3

AtomicDictionaryMBS.GetOrSet(key As Variant, defaultValue as Variant = nil) as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Gets a value or sets it.
Example
Var a As New AtomicDictionaryMBS

a.Value(1) = 1

var r1 as Boolean = a.GetOrSet(1, 2) // gets 1, doesn't set 2
Var r2 As Boolean = a.GetOrSet(2, 3) // sets 3

MessageBox a.Value(1)+" "+a.Value(2) // 1 3

AtomicDictionaryMBS.HasKey(key As Variant) As Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Returns True if key is in the Dictionary and False if it is not.
Example
Var a As New AtomicDictionaryMBS
a.Value(1) = 2

Var r1 As Boolean = a.HasKey(1) // found
Var r2 As Boolean = a.HasKey(2) // key not found

Break

AtomicDictionaryMBS.Increment(key As Variant, Value as Integer = 1) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Increments the integer value for the given key.
Example
Var a As New AtomicDictionaryMBS

var r1 as integer = a.Increment("test")
Var r2 As Integer = a.Increment("test")
Var r3 As Integer = a.Increment("test", 2)

MessageBox a.Value("test").StringValue // shows 4

Returns the new value.

AtomicDictionaryMBS.Iterator as Iterator

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates an iterator for the ´for each´ loops.

Returns AtomicDictionaryIteratorMBS object.

AtomicDictionaryMBS.Key(Index as Integer) as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Returns the value of key for the item in the Dictionary at the index passed.
Example
Var a As New AtomicDictionaryMBS

a.Value("Hello") = 1
a.Value("World") = 2
a.Value(123) = 3

var u as integer = a.KeyCount-1
For i As Integer = 0 To u
MessageBox a.key(i)
Next

Keys are not case-sensitive, but they are encoding-sensitive. If there is no item in the Dictionary at the index passed, a call generates an OutOfBoundsException error.

AtomicDictionaryMBS.Keys as Variant()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Returns an array of keys allowing you to iterate through all the key/value pairs in the Dictionary.
Example
Var a As New AtomicDictionaryMBS

a.Value("Hello") = 1
a.Value("World") = 2
a.Value(123) = 3

Var keys() As Variant = a.keys

Break // inspect in debugger

The order is stable and matches the order returned by the Values method at least until the Dictionary is modified. Use this method with For Each or For...Next to loop through all the keys. Keys are not case-sensitive, but they are encoding-sensitive.

AtomicDictionaryMBS.Lookup(key As Variant, defaultValue As Variant = nil) As Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Looks up the passed value of Key.
Example
Var a As New AtomicDictionaryMBS

a.Value("Hello") = "World"

Var v1 As Variant = a.Lookup("Hello", Nil)
Var v2 As Variant = a.Lookup("test", "test")

Break

If Key is found, it returns the corresponding value. If key is not found, it returns the passed defaultValue.

AtomicDictionaryMBS.Remove(key As Variant)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Removes the value-key pair from the Dictionary with the given key.
Example
Var a As New AtomicDictionaryMBS

a.Value(1) = 1
a.Value(2) = 2
a.Value(3) = 3

a.Remove 2

MessageBox a.KeyCount.ToString // 2

If key is not in the Dictionary, it raises a KeyNotFoundException error.

AtomicDictionaryMBS.RemoveAll

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Removes all entries from the Dictionary.
Example
Var a As New AtomicDictionaryMBS
a.Value(1) = 2
a.Value(2) = 3
Print a.KeyCount.ToString+" entries"
a.RemoveAll
Print a.KeyCount.ToString+" entries after clear"

This invalidates all iterators that were created from the Dictionary.

AtomicDictionaryMBS.SetIfAbsent(key As Variant, Value as Variant) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Sets value if absent.
Example
Var a As New AtomicDictionaryMBS

a.Value(1) = 1

var r1 as Boolean = a.SetIfAbsent(1, 2)
Var r2 As Boolean = a.SetIfAbsent(2, 3)

MessageBox a.Value(1)+" "+a.Value(2) // 1 3

Returns true on success or false on failure.
e.g. for a Job queue to add a job, if it isn't already in the queue.

AtomicDictionaryMBS.SetValues(keys() As Variant, values() As Variant)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Sets a lot of values for given keys in the dictionary.
Example
Var d As New AtomicDictionaryMBS

Var keys() As Variant
keys.Add "a"
keys.Add "b"
keys.Add "c"
Var values() As Variant
values.Add "Apple"
values.Add "Berry"
values.Add "Citron"

d.SetValues keys, values

MessageBox d.KeyCount.ToString+" keys. b = "+d.Lookup("b").StringValue

Both arrays should have the same size.

AtomicDictionaryMBS.Value(key As Variant) As Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
etrieves a value to the key item in the Dictionary.
Example
Var a As New AtomicDictionaryMBS

a.Value(1) = 1

var v as Variant = a.Value(1)

MessageBox v.StringValue

If you attempt to read a value for a key that is not in the Dictionary, a KeyNotFoundException error is raised. To avoid the exception, use the Lookup method to provide a default value if the key is not found or use the HasKey method to check if the key exists.

See also:

AtomicDictionaryMBS.Value(key As Variant, Assigns value As Variant)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Assigns a value to the key item in the Dictionary.
Example
Var a As New AtomicDictionaryMBS

a.Value(1) = 1

var v as Variant = a.Value(1)

MessageBox v.StringValue

See also:

AtomicDictionaryMBS.Values as Variant()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Atomic MBS Util Plugin 26.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Returns all the values in the Dictionary as an array of Variants.
Example
Var a As New AtomicDictionaryMBS

a.Value(1) = "Hello"
a.Value(2) = "World"
a.Value(3) = 123

Var values() As Variant = a.Values

Break // inspect in debugger

The order is stable and matches the order returned by Keys at least until the Dictionary is modified. Use this method with For Each to loop through all the values.

The items on this page are in the following plugins: MBS Util Plugin.


The biggest plugin in space...