Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>VBA has a 'Collection' object built in, and many people consider the 'Dictionary' object from the MS Scripting Runtime to be sufficiently standard that it's essentially part of VBA.</p> <p>However, neither will enforce uniqueness for general objects. Whether or not you can make them enforce uniqueness for your application depends on the details.</p> <p>For example, if you want a set of strings, it's easy. Just use a 'Dictionary', and use its keys as your "set". 'Dictionary' has an 'Exists' method, so it would be pretty easy to write your own limited 'Set' class where all of the real work is done by a contained 'Dictionary'. (If you're using Excel, this will work with any simple Variant value - strings, numbers, booleans, errors.)</p> <p>If you have object instances, things can get more complicated. The 'Dictionary' object <em>can</em> use objects as keys, but it uses object identity as its equality test. For example, I just typed this into the VBA Immediate window:</p> <pre><code>set d=new Dictionary set c1=new Collection set c2=c1 d(c1) = 42 fv d &lt;Dictionary: keys: #V(0..0){&lt;Collection: 0 elems: &gt;}, items: #V(0..0){42%}&gt; d(c2)=99 fv d &lt;Dictionary: keys: #V(0..0){&lt;Collection: 0 elems: &gt;}, items: #V(0..0){99%}&gt; set c3=new Collection d(c3)=101 fv d &lt;Dictionary: keys: #V(0..1){&lt;Collection: 0 elems: &gt;,&lt;Collection: 0 elems: &gt;}, items: #V(0..1){99%,101%}&gt; </code></pre> <p>('fv' is a VBA routine I use for debugging - it just prints out a string representation of stuff.) You can see that identical objects are treated as equal, but identically <em>valued</em> objects aren't.</p> <p>If you need a set that has a custom equality test, you'll need to write non-trivial code. However, if you can at least write a mapping from object instance to a value that can be used as a Dictionary key, you can still avoid having to write your own "is this thing already a member of the set?" code.</p> <p>Some relevane links:</p> <p><a href="https://stackoverflow.com/questions/4487521/is-there-a-way-to-write-an-equality-test-for-a-vba-class-with-private-members-wit">Is there a way to write an equality test for a VBA class with private members without exposing knowledge of the existence of those private members?</a></p> <p><a href="https://stackoverflow.com/questions/1309689/hash-table-associative-array-in-vba/1309739#1309739">Hash Table/Associative Array in VBA</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload