Last active
August 29, 2015 14:07
-
-
Save chuckremes/b6ab0367d611b86e9c6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
'the instrument class | |
Public pName As String | |
Public pDateAdded As Date | |
Public pSide As String | |
Property Get Name() As String | |
'return the instrument's name | |
Name = pName | |
End Property | |
Property Let Name(n As String) | |
pName = n | |
End Property | |
Property Get DateAdded() As Date | |
DateAdded = pDateAdded | |
End Property | |
Property Let DateAdded(d As Date) | |
pDateAdded = d | |
End Property | |
Property Get Side() As String | |
Side = pSide | |
End Property | |
Property Let Side(s As String) | |
pSide = s | |
End Property | |
'this collection could contain anything, but the class | |
'controls access to it and ensures it will just contain | |
'instruments | |
Private pBasket As New Collection | |
Sub Add(Name As String, DateAdded As Date, Side As String) | |
'create a new Instrument and add to collection | |
Dim i As New Instrument | |
i.Name = Name | |
i.DateAdded = DateAdded | |
i.Side = Side | |
pBasket.Add i, Name | |
End Sub | |
Property Get Count() As Long | |
'return the number of instruments | |
Count = pBasket.Count | |
End Property | |
Property Get Item(NameOrNumber As Variant) As Instrument | |
'return this particular instrument | |
Set Item = pBasket(NameOrNumber) | |
End Property | |
Property Get Items() As Collection | |
'return the collection of items | |
Set Items = pBasket | |
End Property | |
Sub Remove(NameOrNumber As Variant) | |
'remove this instrument from collection | |
pBasket.Remove NameOrNumber | |
End Sub | |
Public Function Contains(key As Variant) As Boolean | |
Dim obj As Variant | |
On Error GoTo err | |
Contains = True | |
obj = Me.Item(key) 'always fails! | |
Exit Function | |
err: | |
Contains = False | |
End Function | |
'Print myself out | |
Sub Inspect() | |
Dim ins As Instrument | |
For Each ins In Me.Items | |
Debug.Print ins.Name | |
Next ins | |
End Sub | |
'code to illustrate the breakage | |
Sub test2() | |
Dim coll As New Basket | |
coll.Add "abc", Now, "L" | |
coll.Inspect | |
Debug.Print coll.Item("abc").Name | |
Debug.Print coll.Contains("abc") | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment